Access a static variable by $var::$reference

Posted by chuckg on Stack Overflow See other posts from Stack Overflow or by chuckg
Published on 2009-03-23T23:56:41Z Indexed on 2010/04/27 22:43 UTC
Read the original article Hit count: 141

Filed under:
|

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";
all_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.

Thanks.

© Stack Overflow or respective owner

Related posts about php5

Related posts about php