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


PHP ReflectionClass getConstants()用法及代码示例


ReflectionClass::getConstants()函数是PHP中的内置函数,用于返回指定常量名称的数组。

用法:

array ReflectionClass::getConstants( void )

参数:该函数不接受任何参数。


返回值:此函数返回指定常量名称的数组。

以下示例程序旨在说明PHP中的ReflectionClass::getConstants()函数:

示例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 getConstants() function 
$a = $A->getConstants(); 
  
// Getting an array of the constants 
print_r($a); 
?>

输出:

Array
(
    [First] => GeeksforGeeks
    [Second] => GFG
)

示例2:

<?php 
  
// Declaring a class named as Departments 
class Departments { 
      
    // Defining some constants 
    const First = "CSE"; 
    const Second = "ECE"; 
    const Third = "EE"; 
    const Fourth = "Mechanical"; 
} 
  
// Using the ReflectionClass() function  
// over the Departments class 
$A = new ReflectionClass('Departments'); 
  
// Calling the getConstants() function 
$a = $A->getConstants(); 
  
// Getting an array of the constants 
print_r($a); 
?>

输出:

Array
(
    [First] => CSE
    [Second] => ECE
    [Third] => EE
    [Fourth] => Mechanical
)

参考: https://www.php.net/manual/en/reflectionclass.getconstants.php



相关用法


注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 PHP | ReflectionClass getConstants() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。