Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to test if a variable is static in PHP?

Is it possible to test if a variable is static in PHP? I am trying create a magic method __get that also looks at static variables. I find that property_exists() returns true when a variable is static too. But I will need to use :: instead of -> I'd expect?

like image 251
JM at Work Avatar asked Jul 04 '11 08:07

JM at Work


1 Answers

It is possible to test if a variable is static via Reflection:

class Foo { static $bar; }
$prop = new ReflectionProperty('Foo', 'bar');
var_dump($prop->isStatic()); // TRUE

However, that still won't allow you to use them with magic methods __get or __set, because those only work in object context. From the PHP Manual on Magic Methods:

Property overloading only works in object context. These magic methods will not be triggered in static context. Therefore these methods should not be declared static. As of PHP 5.3.0, a warning is issued if one of the magic overloading methods is declared static.

Also see this discussion on the PHP Internals Mailing List about introducing __getStatic:

  • http://marc.info/?l=php-internals&m=121875353105996&w=1
like image 175
Gordon Avatar answered Sep 23 '22 02:09

Gordon