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

I have it:

  • one string variable that contains the class name ( $classname )
  • one string variable with the property name ( $propertyname )

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

If the property was not static, this would be:

 $classname->$propertyname; 

If the property was a method, I could use the call_user_function function

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

But in my case, I just lost. However, I hope this is possible. With thousands of functions that PHP has, it also has something to do with it. Maybe I missed something?

Thank!

Edit:

  • for those who have eval () solutions: thanks, but that is out of the question.
  • for those who get _class _vars () solutions: thanks, but it seems like it returns "the default properties of this class" (php.net), and yes, I would like this value to change (even if it helps me in some cases)
+41
oop properties php static dynamic
Aug 14 '09 at 17:23
source share
11 answers

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

 $classname::$$propertyname; 

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

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

Strike>




EDIT: I just said get_class_vars() will not work if this value is dynamic, but apparently the static static members are part of the "class default properties". You can use the following shell:

 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 a variable, you still have to use eval() , but with some checking 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() , and the check should be safe. If people start putting random things like $className and $property , they will exit the function, because it will not be an existing class or property. Starting with $value , it is never parsed as code, so anything they put there will not affect the script.

+64
Aug 14 '09 at 17:31
source share

I think this is the simplest:

 $foo = new ReflectionProperty('myClassName', 'myPropertyName'); print $foo->getValue(); 
+13
Feb 17 '10 at 12:12
source share

To return the value of a variable given by Static variable, you need to call:

 $static_value = constant($classname.'::'.$propertyname); 

View Documentation :: Permanent PHP Documentation

+9
Oct 27 2018-11-11T00:
source share

One thing that I noticed is that you cannot set variables that are protected in static classes, as the eval () command runs in an area outside the class. The only thing you can get around is to implement a static method inside / of each class that runs eval (). This method can be protected, since call_user_func () [to call the setter method] is also run from within the class.

+1
Feb 01 '10 at 17:38
source share

You should be able to do something like:

 eval("echo $classname::$propertyname;"); 

I just did a quick test and made it work for me. Not sure if there is a better way or not, but I could not find it.

0
Aug 14 '09 at 17:29
source share

'eval' looks so close to 'evil' and I hate using it and / or seeing it in code. Having a few ideas from the other answers, here you can avoid this, even if your php is not 5.3 or higher.

Modified to reflect testing based on commentary.

 class A { static $foo = 'bar'; } A::$foo = 'baz'; $a = new A; $class = get_class($a); $vars = get_class_vars($class); echo $vars['foo']; 

Prints "baz".

0
Aug 14 '09 at 17:37
source share

Potentially relevant: discussion of late static binding in PHP - When do you need to use late static binding? .

0
Aug 14 '09 at 18:06
source share

get_class_vars does not match get_object_vars .

I think get_clas_vars should return the original property values.

0
Nov 07 '09 at 3:47
source share

Even if you said that eval out of the question, prior to PHP 5.3, the easiest solution still uses eval :

 eval("\$propertyval = $classname::\$propertyname;"); echo $propertyval; 
0
Jan 23 '12 at 20:37
source share

You can use ReflectionClass:

 class foo { private static $bar = "something"; } $class = "foo"; $reflector = new ReflectionClass($class); $static_vars = $reflector->getStaticProperties(); var_dump($static_vars["bar"]); 
0
Apr 09 '14 at 13:37
source share

Getting and setting static and non-static properties without using Reflection

Using Reflection works, but it's expensive.

Here is what I use for this purpose,

It works for PHP 5 >= 5.1.0 because I use property_exist

 function getObjectProperty($object, $property) { if (property_exists(get_class($object), $property)) { return array_key_exists($property, get_object_vars($object)) ? $object->{$property} : $object::$$property; } } function setObjectProperty($object, $property, $value) { if (property_exists(get_class($object), $property)) { array_key_exists($property, get_object_vars($object)) ? $object->{$property} = $value : $object::$$property = $value; } } 
-one
Aug 24 '12 at 4:18
source share



All Articles