Page 1 of 1

get static prop from a string get_class('User')::$value

Posted: Tue Mar 16, 2010 6:59 am
by peanutbutter
I always get unexpected T_PAAMAYIM_NEKUDOTAYIM

tried this also
$bar = "User";
$foo = new $bar;
$foo::$value;

the whole reason im trying to do this is because i dont have late static binding

Re: get static prop from a string get_class('User')::$value

Posted: Tue Mar 16, 2010 7:31 am
by AbraCadaver
Obviously what you're doing won't work, but you haven't really shown enough code for anyone to tell what you're trying to do.

If you've already instantiated $foo, then:

Code: Select all

echo $foo->value;
If static, then this will work in PHP 5.3 I think:

Code: Select all

echo $foo::$$value;
Your stuck will eval() before 5.3:

Code: Select all

eval("echo $foo::$value;");

Re: get static prop from a string get_class('User')::$value

Posted: Tue Mar 16, 2010 9:24 am
by Weirdan

Code: Select all

 
function getStaticValue($class, $var) {
  $clz = new ReflectionClass($class);
  return $clz->getStaticPropertyValue($var);
}
var_dump(getStaticValue('User', 'value'));