Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isset on static class attributes

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?

like image 693
Florian Avatar asked Jan 24 '26 08:01

Florian


2 Answers

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();
like image 190
NikiC Avatar answered Jan 27 '26 00:01

NikiC


In 5.3, you can simply do

var_dump(property_exists($class, $attribute));
like image 42
John Cartwright Avatar answered Jan 26 '26 23:01

John Cartwright



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!