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


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


ReflectionClass::getReflectionConstant()函數是PHP中的內置函數,用於返回指定類屬性的ReflectionClassConstant。

用法:

ReflectionClassConstant ReflectionClass::getReflectionConstant( string $name )

參數:該函數接受單個參數名稱,該名稱是類常量的名稱。


返回值:此函數返回指定類的屬性的ReflectionClassConstant。

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

示例1:

<?php 
  
// Declaring a class named as Company 
class Company { 
      
    // Defining some constants 
    const First = "GeeksforGeeks"; 
    const Second = "GFG"; 
} 
  
// Using the ReflectionClass() function  
// over the Company class 
$A = new ReflectionClass('Company'); 
  
// Calling the getReflectionConstant() function 
// over the constant 'First' 
$a = $A->getReflectionConstant('First'); 
  
// Getting the ReflectionClassConstant 
print_r($a); 
?>

輸出:

ReflectionClassConstant Object
(
    [name] => First
    [class] => Company
)

示例2:

<?php 
   
// Declaring a user-defined class Departments 
class Departments { 
       
    // Defining some constants 
    const D1 = "EE"; 
    const D2 = "Civil"; 
    const D3 = "CSE"; 
    const D4 = "IT"; 
} 
   
// Using the ReflectionClass() function  
// over the Departments class 
$A = new ReflectionClass('Departments'); 
   
// Calling the getReflectionConstant() function 
// over specified constants 
$a1 = $A->getReflectionConstant('D1'); 
$a2 = $A->getReflectionConstant('D2'); 
$a3 = $A->getReflectionConstant('D3'); 
$a4 = $A->getReflectionConstant('D4'); 
   
// Getting the ReflectionClassConstant 
print_r($a1); 
print_r($a2); 
print_r($a3); 
print_r($a4); 
?>

輸出:

ReflectionClassConstant Object
(
    [name] => D1
    [class] => Departments
)
ReflectionClassConstant Object
(
    [name] => D2
    [class] => Departments
)
ReflectionClassConstant Object
(
    [name] => D3
    [class] => Departments
)
ReflectionClassConstant Object
(
    [name] => D4
    [class] => Departments
)

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



相關用法


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