函数名称:ReflectionClass::getReflectionConstants()
适用版本:PHP 5 >= 5.1.0, PHP 7
函数描述:该函数用于获取类中定义的常量的反射信息。
用法:
public array ReflectionClass::getReflectionConstants ( void )
参数:无
返回值:该方法返回一个包含ReflectionConstant对象的数组,每个对象代表一个类常量的反射信息。
示例:
class MyClass {
const MY_CONST_1 = 1;
const MY_CONST_2 = 2;
}
$reflectionClass = new ReflectionClass('MyClass');
$constants = $reflectionClass->getReflectionConstants();
foreach ($constants as $constant) {
echo $constant->getName() . " = " . $constant->getValue() . "\n";
}
输出结果:
MY_CONST_1 = 1
MY_CONST_2 = 2
上述示例中,我们定义了一个名为MyClass
的类,并在该类中定义了两个常量MY_CONST_1
和MY_CONST_2
。通过创建ReflectionClass对象并使用getReflectionConstants()
方法,我们可以获取到这两个常量的反射信息。然后,我们使用foreach循环遍历反射常量数组,并分别输出常量的名称和值。最终,输出结果为MY_CONST_1 = 1
和MY_CONST_2 = 2
。