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');
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; eval($className.'::$'.$property.'=$value;'); return true; } class Foo { static $bar = 'Fizz'; } echo get_user_prop('Foo', 'bar');
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.
Andrew Moore Aug 14 '09 at 17:31 2009-08-14 17:31
source share