开发者

Accessing a member of a "variable" class [duplicate]

开发者 https://www.devze.com 2023-02-24 20:59 出处:网络
This question already has answers here: 开发者_StackOverflow社区 Reference - What does this error mean in PHP?
This question already has answers here: 开发者_StackOverflow社区 Reference - What does this error mean in PHP? (38 answers) Closed 10 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消