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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peanutbutter
Forum Newbie
Posts: 1
Joined: Tue Mar 16, 2010 6:54 am

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

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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;");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

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

Post by Weirdan »

Code: Select all

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