[PHP] Re: runtime access to static variable
Unfortunately, in the inherited DeployTask::execute(), self::$STEPS does not refer to UpdateTask::$STEPS, it refers to DeployTask::$STEPS Use parent::$STEPS and call it done? Yes, I know, if you add another layer of class in between, then it's not parent:: anymore, but it seems a bit less convoluted than the reflection solution, to this naive user... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: runtime access to 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..) The full problem is: I started off with a DeployTask for deploying a new instance of my web project to DreamHost: http://cgi.sfu.ca/~jdbates/tmp/php/200812170/DeployTask.class.phps (It is a task in the symfony framework, but basically the DeployTask::execute() method gets called) The task takes about thirty minutes to run, so I broke it up into steps. After each step, it updates a database row so that a user who started the task through a web interface can monitor its progress. Great so far. Now I decided to create an UpdateTask for updating deployed instances to the latest version of my code: http://cgi.sfu.ca/~jdbates/tmp/php/200812170/UpdateTask.class.phps Of course it also takes a long time to run, so my first iteration just extends the DeployTask and defines different steps. UpdateTask inherits DeployTask::execute(), which drives the steps and updates the database. Unfortunately, in the inherited DeployTask::execute(), self::$STEPS does not refer to UpdateTask::$STEPS, it refers to DeployTask::$STEPS : ( I gather with late static bindings in PHP 5.3, static::$STEPS does what I want? Anyway, DeployTask::execute() is not static, so I know whether $this is an instance of DeployTask of UpdateTask: get_class($this) returns it. Which brought me to my contrived example: ket% cat test.php ?php class Test { public static $STEPS = array( 'foo', 'bar'); } $className = 'Test'; var_dump($className::$STEPS); ket% Except that in real life, $className = get_class($this) Check this out: http://us2.php.net/manual/en/language.oop5.static.php It actually won't work until 5.3.0 when they add late static binding. I ran my contrived example with PHP 5.3alpha3, and it worked! $className::$STEPS is not a parse error in 5.3, which is cool : ) Except that in 5.3, I will probably just use static::$STEPS anyway : P In the meantime, I will probably use reflection as suggested: // HACK: Use static::$STEPS in PHP 5.3: // http://php.net/oop5.late-static-bindings $class = new ReflectionClass($this); $steps = $class-getStaticPropertyValue('STEPS'); -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: runtime access to static variable
Jack Bates wrote: 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..) The full problem is: I started off with a DeployTask for deploying a new instance of my web project to DreamHost: http://cgi.sfu.ca/~jdbates/tmp/php/200812170/DeployTask.class.phps (It is a task in the symfony framework, but basically the DeployTask::execute() method gets called) The task takes about thirty minutes to run, so I broke it up into steps. After each step, it updates a database row so that a user who started the task through a web interface can monitor its progress. Great so far. Now I decided to create an UpdateTask for updating deployed instances to the latest version of my code: http://cgi.sfu.ca/~jdbates/tmp/php/200812170/UpdateTask.class.phps Of course it also takes a long time to run, so my first iteration just extends the DeployTask and defines different steps. UpdateTask inherits DeployTask::execute(), which drives the steps and updates the database. Unfortunately, in the inherited DeployTask::execute(), self::$STEPS does not refer to UpdateTask::$STEPS, it refers to DeployTask::$STEPS : ( Yup I follow, been here myself too; infact doing very similar at the mo making an event handler / listener for the hell of it! anyhow.. stick this in you're DeployTask and run it :p protected function execute($arguments = array(), $options = array()) { // you're existing code here up to.. $job-pid = getmypid(); $thisClass = new ReflectionClass( get_class($this) ); $steps = $thisClass-getStaticPropertyValue('STEPS'); while ($job-step count($steps)) { $job-save(); call_user_func(array($this, $steps[$job-step]), $arguments, $options); $job-step++; } $job-delete(); } untested but should work :) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: runtime access to static variable
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') ); ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: runtime access to static variable
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 is also a not great but usable, stricter and faster solution: ?php class TestClass { public static $STEPS = array( 'foo' , 'bar' ); public function steps() { return self::getSteps(); } public static function getSteps() { return self::$STEPS; } public static function setSteps( $steps ) { self::$STEPS = $steps; } } $testClass = new TestClass; print_r( $testClass-steps() ); ? really though why..? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php