Re: runtime access to static variable

From: Date: Tue, 16 Dec 2008 21:22:10 +0000
Subject: Re: runtime access to static variable
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-284687@lists.php.net to get a copy of this message
Jack Bates wrote:
How do I access a static variable when I do not know the name of the class until runtime? I have the following example PHP: ket% cat test.php <?php class Test { public static
    $STEPS = array(
      'foo',
      'bar');
} $className = 'Test'; var_dump($className::$STEPS); ket% Unfortunately when I run it I get: ket% php test.php Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /home/jablko/trash/test.php on line 13 ket% I can call a static function using call_user_func(array($className, 'functionName')), and I can access a class constant using constant($className.'::CONSTANT_NAME'). How do I access a static variable?
this does beg the question why don't you know the classname at runtime.. seems to be a slight design flaw and may make sense for you to post the full problem (you must have chosen to implement this for a reason..) if you really really must do this, you'd be best off to have a look at reflection.. <?php class TestClass { public static $STEPS = array( 'foo' , 'bar' ); } $testClass = new TestClass; $rTestClass = new ReflectionClass( get_class($testClass) ); print_r( $rTestClass->getStaticPropertyValue('STEPS') ); ?>

« previous php.general (#284687) next »