tags:

views:

82

answers:

4

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.

+3  A: 

Which version of PHP are you running? I believe above 5.3.x this is allowed but before that it isn't.

EDIT: here you go as of PHP 5.3.0 it's allowed Example #2

echo $classname::doubleColon(); // As of PHP 5.3.0

Edit: For variables use

echo $classname::$variable; // PHP 5.3.0 +

here's the link

Edit 3: Try this link the answer from there seems like it would apply to your situation.

Viper_Sb
What's with the `()`? This is a variable not a method.
On the page there they list methods, but it's the same for variables. With variables you also need $ in front of the name
Viper_Sb
Hmmm. Odd that in 5.2.X I can do `new $className()` but not `$className::$staticVar`. Oh well.
Nathan Long
Thanks for pointing me in the right direction. Turns out that reflection handles this fine in PHP 5 and up! See my answer.
Nathan Long
+1  A: 
BoltClock
Are you saying you can't access static variables prior to 5.3?!
No, I'm saying we can't use the `$className::$someStaticVar` syntax to access static variables of a variable class name prior to 5.3.
BoltClock
Thanks - updated my example in the question.
Nathan Long
@BoltClock My point was he only needs to worry about late static bindings if $className is a class inheriting a static variable. If $className is the class where the static variable is declared then `$className::$someStaticVar` is the correct syntax right and will work prior to 5.3 as late static binding isn't required. (I'm asking more for my own understanding of this issue and to clarify the point.)
@BoltClock - just found out that reflection works for this - see my answer. (Others have said so, too, but I wanted to give a clear example.)
Nathan Long
+1  A: 

You might have to use the reflection classes. http://www.php.net/manual/en/reflectionfunctionabstract.getstaticvariables.php

Or use a simple string eval: print "{$className::$someStaticVar}", which replaces $className before looking up the ::$someStaticVar. Not sure about PHP < 5.2 though.

mario
+3  A: 

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.

$className = 'SomeClass';

$SomeStaticProperty = new ReflectionProperty($className, 'propertyName'); 
echo $SomeStaticProperty->getValue();

See http://www.php.net/manual/en/class.reflectionproperty.php

A similar trick works for methods.

$Fetch_by_id = new ReflectionMethod($someDbmodel,'fetch_by_id');
$DBObject = $Fetch_by_id->invoke(NULL,$id);
// Now you can work with the returned object
echo $DBObject->Property1;
$DBObject->Property2 = 'foo';
$DBObject->save();

See http://php.net/manual/en/class.reflectionmethod.php and http://www.php.net/manual/en/reflectionmethod.invoke.php

Nathan Long