Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting static property from a class with dynamic class name in PHP

I have this:

  • one string variable which holds the class name ($classname)
  • one string variable with holds the property name ($propertyname)

I want to get that property from that class, the problem is, the property is static and I don't know how to do that.

If the property weren't static, it would have been:

$classname->$propertyname; 

if the property were a method, I could have used call_user_function

call_user_func(array($classname, $propertyname)); 

But in my case, am I just lost. I am however hoping that it is possible. With the thousands of functions that PHP has, he'd better have something for this as well. Maybe I'm missing something?

Thanks!

Edit:

  • for those with eval() solutions: thanks, but it is out of the question
  • for those with get _class _vars() solutions: thanks, but it seems it returns "the default properties of the given class" (php.net), and yes, I would like that value to be changable (even though it does help me in some of the cases)
like image 709
treznik Avatar asked Aug 14 '09 17:08

treznik


People also ask

How can I get static properties in PHP?

Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ). print $foo::$my_static .

Can we inherit static class in PHP?

In PHP, if a static attribute is defined in the parent class, it cannot be overridden in a child class.

How can we create static property and class method in PHP?

to use static propery inside any method of the same class, use self keyword instead of -> operator that is used for accessing instance properties. <? php class testclass{ static $name="test"; public function test(){ echo self::$name; } } $obj=new testclass(); $obj->test(); ?>

Can we change static variable value in PHP?

No. Static can be used to declare class variables or within function to declare a variable which persists over function calls, but not over executions of the script.


1 Answers

If you are using PHP 5.3.0 or greater, you can use the following:

$classname::$$propertyname; 

Unfortunately, if you are using a version lower than 5.3.0, you are stuck using eval() (get_class_vars() will not work if the value is dynamic).

$value = eval($classname.'::$'.$propertyname.';'); 


EDIT: I've just said get_class_vars() wouldn't work if the value is dynamic, but apparently, variable static members are part of "the default properties of a class". You could use the following wrapper:

function get_user_prop($className, $property) {   if(!class_exists($className)) return null;   if(!property_exists($className, $property)) return null;    $vars = get_class_vars($className);   return $vars[$property]; }  class Foo { static $bar = 'Fizz'; }  echo get_user_prop('Foo', 'bar'); // echoes Fizz Foo::$bar = 'Buzz'; echo get_user_prop('Foo', 'bar'); // echoes Buzz 

Unfortunately, if you want to set the value of the variable, you will still need to use eval(), but with some validation in place, it's not so evil.

function set_user_prop($className, $property,$value) {   if(!class_exists($className)) return false;   if(!property_exists($className, $property)) return false;    /* Since I cannot trust the value of $value    * I am putting it in single quotes (I don't    * want its value to be evaled. Now it will    * just be parsed as a variable reference).    */   eval($className.'::$'.$property.'=$value;');   return true; }  class Foo { static $bar = 'Fizz'; }  echo get_user_prop('Foo', 'bar'); // echoes Fizz set_user_prop('Foo', 'bar', 'Buzz'); echo get_user_prop('Foo', 'bar'); // echoes Buzz 

set_user_prop() with this validation should be secure. If people start putting random things as $className and $property, it will exit out of the function as it won't be an existing class or property. As of $value, it is never actually parsed as code so whatever they put in there won't affect the script.

like image 155
Andrew Moore Avatar answered Sep 28 '22 07:09

Andrew Moore