0

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
  • 1
    FYI: "PAAMAYIM NEKUDOTAYIM" is apparently Hebrew for "double colon". PHP also defines the parser token `T_DOUBLE_COLON`, as an alias I guess, but the Hebrew version is a well known joke by now. – AgentConundrum Apr 13 '11 at 20:59

2 Answers2

2

Use get_class_vars

$values = get_class_vars($class);

echo $values["default_error_message"];

CodePad Demo

Emanuil Rusev
  • 34,563
  • 55
  • 137
  • 201
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
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;
}

Also:

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

Frank Farmer
  • 38,246
  • 12
  • 71
  • 89