Flo*_*ian 3 php oop static static-members isset
class A {
public static $foo = 42;
}
$class = 'A';
$attribute = 'foo';
var_dump(isset($class::$attribute)); //gives bool(false)
Run Code Online (Sandbox Code Playgroud)
我怎么能检查这个静态属性是否存在于这个类中?
使用变量变量:
var_dump(isset($class::$$attribute)); // the two dollars are intentional
Run Code Online (Sandbox Code Playgroud)
如果你没有PHP 5.3,唯一准确的方法可能是使用Reflection API:
$reflectionClass = new ReflectionClass($class);
$exists = $reflectionClass->hasProperty($attribute) && $reflectionClass->getProperty($attribute)->isStatic();
Run Code Online (Sandbox Code Playgroud)