A variable named $class
contains the name of a class.
How can I access a static member of that class?
I need an approach that would work in PHP 5.2.
The following works in PHP 5.3:
$class::$default_error_message;
In PHP 5.2 it outputs:
unexpected T_PAAMAYIM_NEKUDOTAYIM
Btw, T_PAAMAYIM_NEKUDOTAYIM
?! PHP doesn't cease to amaze me.
Use get_class_vars
$values = get_class_vars($class);
echo $values["default_error_message"];
CodePad Demo
function getStaticMember($class, $member) {
if(is_object($class))
$class = get_class($class);
$classObj = new ReflectionClass($class);
$result = null;
foreach($classObj->getStaticProperties() as $prop => $value) {
if($prop == $member) {
$result = $value;
break;
}
}
return $result;
}
Also:
In PHP, the scope resolution operator is also called Paamayim Nekudotayim (Hebrew: פעמיים נקודתיים), which means "twice colon" or "double colon" in Hebrew.
精彩评论