0

Possible Duplicate:
From the string name of a class, can I get a static variable?

Somewhere in a parent class, I need to find the value of a static variable of one of the possible child classes, determined by the current instance.

I wrote:

  $class = get_class($this);
  $value = isset($class::$foo['bar']) ? $class::$foo['bar'] : 5;

In this example, the subclass whose name is in $class has a public static $foo.

I know using $class::$foo['bar'] is not a very beautiful piece of code, but it gets the job done on PHP 5.3.4.

In PHP 5.2.6 though, I am getting a syntax error:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting ',' or ')'

Is there an alternative way that would work on PHP 5.2.4+ that would get the same thing done?

Community
  • 1
  • 1
Pelle
  • 6,423
  • 4
  • 33
  • 50
  • Good catch. Fixed it by using the solution provided there. – Pelle Apr 14 '11 at 14:55
  • Hmm, original comment was removed. By "the solution provided there" I refer to the question that this is a duplicate of. See link above. – Pelle Apr 14 '11 at 15:03
  • This kind of comments is created automatically when someone votes to close a question as duplicate. If the question is closed, the comment gets removed and the link is added to the top of the question, as you already saw... – Felix Kling Apr 14 '11 at 15:04
  • Yep, I just found out. As that auto-comment disappeared when I voted to close my own question (i.e. putting up the last straw) my "reply" looked rather useless. Thanks for the explaination. – Pelle Apr 14 '11 at 15:06

4 Answers4

1

EDIT: Reflection is better.

You can try the get_class_vars method. No access to PHP 5.2.6, but this works in 5.2.11...

class Test {
    public static $foo;

    function __construct() {
        echo("...Constructing...<br/>");
        Test::$foo = array();
        Test::$foo['bar'] = 42;
    }

    function __toString() {
        return "Test";
    }
}


$className = 'Test';
$class = new $className();

$vars = get_class_vars($className);

echo($vars['foo']['bar'] . "<br/>");

Output:

...Constructing...
42
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
1

The reason that this does not work in PHP 5.2, is because before PHP 5.3 you are not allowed to use variables in the classname. So, if possible use eval for this.

eval('$result = ' . $c . '::$foo[\'bar\'];');
echo $result;

Otherwise, you're forced to use a function in the child class to receive the value. For example:

class MyParent {
    public function __construct() {
        $var = $this->_getVariable();
        echo $var['bar'];
    }
}

class MyChild extends MyParent {
    static $var = array('bar' => 'foo');

    protected function _getVariable() {
        return self::$var;
    }
}

new MyChild();
user228395
  • 1,156
  • 1
  • 11
  • 25
  • While this would work, I fixed it using the ReflectionProperty class in the SPL, which is accessible in PHP 5.2. – Pelle Apr 14 '11 at 15:01
0
class Bar1 {
    static $var = array('index' => 'value');
}
class Bar2 extends Bar1 {
}
class Foo extends Bar2 {
    static $var = array('index' => 'value in Foo');
    public function __construct() {
        echo parent::$var['index'];
    }
}

$foo = new Foo();

will output 'value', but not 'value in Foo'

Hope, that's what you are looking for.

Nemoden
  • 8,816
  • 6
  • 41
  • 65
  • No, I specifically want to access a variable in a subclass, not one in the superclass. – Pelle Apr 14 '11 at 14:58
  • **Pelle ten Cate**, that's exactly what my code does. see updated code – Nemoden Apr 14 '11 at 15:05
  • I might be messing up terminology than. You are especially accessing a variable of Bar1 in class Foo. I wanted to do the opposite: assume both Foo and Bar2 has a public static variable $var, I want to access that in a function in class Bar1 that was called in one of the other classes. – Pelle Apr 14 '11 at 15:27
-1

You can get class static/call static method in the class you are working in using self key word or for parent class using parent. You can get that error on php 5.2.6 because of changes in get_class function in PHP 5.3.0

Robik
  • 6,047
  • 4
  • 31
  • 41
  • No, the error is about late static binding, a syntax that was introduced in PHP 5.3. The behaviour of get_class hasn't changed if a parameter was specified. As said, I was looking for getting a variable in one of the subclasses, not in the superclass (`parent`) or `self`. – Pelle Apr 14 '11 at 15:00