I am trying to access a static variable within a class by using a variable class name. I’m aware that in order to access a function within the class, you use call_user_func():
class foo { function bar() { echo 'hi'; } } $class = 'foo'; call_user_func(array($class, 'bar')); // prints hi
However, this does not work when trying to access a static variable within the class:
class foo { public static $bar = 'hi'; } $class = 'foo'; call_user_func(array($class, 'bar')); // nothing echo $foo::$bar; // invalid
How do I get at this variable? Is it even possible? I have a bad feeling this is only available in PHP 5.3 going forward and I’m running PHP 5.2.6.
You can use reflection to do this. Create a ReflectionClass object given the classname, and then use the getStaticPropertyValue method to get the static variable value.