PHP:使用ReflectionClass从静态方法获取类常量
我试图从静态方法中获取类常量列表PHP:使用ReflectionClass从静态方法获取类常量,php,reflection,static,Php,Reflection,Static,我试图从静态方法中获取类常量列表 public static function example() { $reflection = new \ReflectionClass(get_called_class()); var_dump($reflection -> getConstants()); } 抛出致命错误:当没有活动的类作用域时无法访问self:: 这有什么办法可以实现吗,或者我在php中遇到了另一个语言限制吗?我刚刚尝试了你的代码,效果很好。你能提供一个实际类的示例吗
public static function example()
{
$reflection = new \ReflectionClass(get_called_class());
var_dump($reflection -> getConstants());
}
抛出致命错误:当没有活动的类作用域时无法访问self::
这有什么办法可以实现吗,或者我在php中遇到了另一个语言限制吗?我刚刚尝试了你的代码,效果很好。你能提供一个实际类的示例吗
class test23 {
const te = 'asd';
var $ya = 'hoopla';
public static function example()
{
$reflection = new ReflectionClass(get_called_class());
var_dump($reflection -> getConstants());
}
}
test23::example()代码>返回数组(1){[“te”]=>string(3)“asd”}
希望能有帮助
// creates a reflection class object
$reflection = new ReflectionClass ( $this );
//gets all the constants of the current class
$consts = $reflection->getConstants ();