Given the string name of a class in PHP, how can I access one of its static variables?
What I’d like to do is this:
$className = 'SomeClass'; // assume string was actually handed in as a parameter
$foo = $className::$someStaticVar;
…but PHP gives me a lovely “Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”, which apparently is a Hebrew name for the double colon(::).
Update: Unfortunately, I have to use PHP 5.2.X for this.
Update 2: As MrXexxed guessed, the static variable is inherited from a parent class.
Reflection will do it
A coworker just showed me how to do this with reflection, which works with PHP 5 (we’re on 5.2), so I thought I’d explain.
See http://www.php.net/manual/en/class.reflectionproperty.php
A similar trick works for methods.
See http://php.net/manual/en/class.reflectionmethod.php and http://www.php.net/manual/en/reflectionmethod.invoke.php