当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。