牛牛在线工具

ReflectionClass::getConstant()

收录 8868 个 PHP 函数,含用途说明与用法示例。

ReflectionClass::getConstant()

获取指定类的常量值

函数:ReflectionClass::getConstant() 适用版本:PHP 5 >= 5.1.0, PHP 7 用法:ReflectionClass::getConstant() 方法用于获取指定类的常量值。 语法:public ReflectionClass::getConstant(string $name): mixed 参数: $name:要获取的常量名称。 返回值:获取到的常量值,如果常量不存在则返回 NULL。 示例: class MyClass { const MY_CONSTANT = 123; } $reflection = new ReflectionClass('MyClass'); $constantValue = $reflection->getConstant('MY_CONSTANT'); var_dump($constantValue); // 输出 int(123) 在上面的示例中,我们首先定义了一个名为MyClass的类,并在该类中定义了一个常量MY_CONSTANT,其值为123。 然后,我们使用ReflectionClass类创建了一个反射对象$reflection,并将MyClass作为参数传递给它。 接下来,我们使用$reflection对象的getConstant()方法来获取MyClass类中名为MY_CONSTANT的常量的值,并将其赋值给$constantValue变量。 最后,我们使用var_dump()函数输出了$constantValue的值,结果为int(123),表示成功获取到了常量的值。 请注意,getConstant()方法只能用于获取类的常量值,而不能用于获取类的属性值或方法。