访问“变量”类的成员

时间:2011-04-13 20:53:17

标签: php

名为$class的变量包含类的名称。

如何访问该类的静态成员?

我需要一种适用于PHP 5.2的方法。


以下适用于PHP 5.3:

$class::$default_error_message;

在PHP 5.2中输出:

unexpected T_PAAMAYIM_NEKUDOTAYIM

顺便说一下,T_PAAMAYIM_NEKUDOTAYIM?! PHP不会让我感到惊讶。

2 个答案:

答案 0 :(得分:2)

使用get_class_vars

$values = get_class_vars($class);

echo $values["default_error_message"];

CodePad Demo

答案 1 :(得分:1)

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;
}

此外:

  

In PHP, the scope resolution operator is also called Paamayim Nekudotayim (Hebrew: פעמיים נקודתיים‎), which means "twice colon" or "double colon" in Hebrew.