isset on static class attributes

戏子无情 提交于 2020-01-04 03:12:13

问题


class A {
    public static $foo = 42;
}

$class = 'A';
$attribute = 'foo';

var_dump(isset($class::$attribute)); //gives bool(false)

How can i checkt, of this static attribute exists in this class?


回答1:


Use variable variables:

var_dump(isset($class::$$attribute)); // the two dollars are intentional

If you don't have PHP 5.3 yet the only accurate way is probably using the Reflection API:

$reflectionClass = new ReflectionClass($class);
$exists = $reflectionClass->hasProperty($attribute) && $reflectionClass->getProperty($attribute)->isStatic();



回答2:


In 5.3, you can simply do

var_dump(property_exists($class, $attribute));


来源:https://stackoverflow.com/questions/5771797/isset-on-static-class-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!