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?
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?
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();
$class::
syntax was introduced in PHP 5.3. –
Dnieper $$class::$$attribute
. –
Wilen property_exists($class, $attribute)
is clearer, though it won't distinguish between static and non-static members, and it only works on non-public members as of 5.3. –
Catricecatrina T_PAAMAYIM_NEKUDOTAYIM
is still only class_name
or reference_variable
. $$class
would need at least a variable_without_objects
;) –
Dnieper get_class_vars
might be useful. –
Catricecatrina In 5.3, you can simply do
var_dump(property_exists($class, $attribute));
© 2022 - 2024 — McMap. All rights reserved.