當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP ReflectionClass getConstant()用法及代碼示例


ReflectionClass::getConstant()函數是PHP中的內置函數,用於返回定義的常量的值。

用法:

mixed ReflectionClass::getConstant( string $name )

參數:此函數接受參數Name,它是已定義常量的名稱。


返回值:此函數返回定義的常量的值。

以下示例程序旨在說明PHP中的ReflectionClass::getConstant()函數:

示例1:

<?php 
  
// Declaring a class named as Department 
class Department { 
      
    // Defining a constant 
    const First = "CSE"; 
      
} 
  
// Using the ReflectionClass() function  
// over the Department class 
$A = new ReflectionClass('Department'); 
  
// Calling the getConstant() function over 
// First constant 
$a = $A->getConstant('First'); 
  
// Getting the value of the defined constant 
print_r($a); 
?>

輸出:

CSE

示例2:

<?php 
    
// Declaring a class named as Company 
class Company { 
        
    // Defining a constant 
    const First = "GeeksforGeeks"; 
    const Second = "GFG"; 
    const Third = "gfg"; 
        
} 
    
// Using the ReflectionClass() function  
// over the Company class 
$A = new ReflectionClass('Company'); 
    
// Calling the getConstant() function over 
// the defined constants and 
// getting the value of the defined constants 
print_r($A->getConstant('First')); 
echo("\n"); 
print_r($A->getConstant('Second')); 
echo("\n"); 
print_r($A->getConstant('Third')); 
?>
輸出:
GeeksforGeeks
GFG
gfg

參考: https://www.php.net/manual/en/reflectionclass.getconstant.php



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 PHP | ReflectionClass getConstant() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。