10

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Nathan Long
  • 122,748
  • 97
  • 336
  • 451

4 Answers4

15

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

Community
  • 1
  • 1
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
8

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.

Community
  • 1
  • 1
Viper_Sb
  • 1,809
  • 14
  • 18
  • What's with the `()`? This is a variable not a method. –  Jul 28 '10 at 15:51
  • 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 Jul 28 '10 at 15:54
  • Hmmm. Odd that in 5.2.X I can do `new $className()` but not `$className::$staticVar`. Oh well. – Nathan Long Jul 28 '10 at 16:04
  • 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 Jul 29 '10 at 17:24
  • A gotcha is that if your classname is the property of a class (e.g. `$this->classname`) you need to go `$classname = $this->classname` and then attempt to access the static property or method of your new variable `$classname`. – jcroll Oct 27 '14 at 21:20
1

That's only possible in PHP 5.3 and later with late static bindings.

The workaround for older versions of PHP that first comes to my mind is — please don't hurt me — using eval():

if (class_exists($className))
{
    eval('$foo = ' . $className . '::$someStaticVar;');
}

By the way, when accessing static variables, the $ before the variable name is needed, as in $someStaticVar.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Are you saying you can't access static variables prior to 5.3?! –  Jul 28 '10 at 15:50
  • 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 Jul 28 '10 at 15:51
  • 1
    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.) –  Jul 28 '10 at 16:17
  • 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 Jul 29 '10 at 15:17
1

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
  • 144,265
  • 20
  • 237
  • 291