PHP Late Static Binding — Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE

StackOverflow https://stackoverflow.com/questions/11549787

  •  21-06-2021
  •  | 
  •  

Question

Testing some late static binding and getting this error on line 5:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE

line 5:

protected static test = 'A TEST';

Here is the source:

class A {

    protected static test = 'A TEST';

    public static function test() {
        echo $this->test;
    }
}

Class B extends A {
    public static test = "B TEST";
    public function static_test() {
        echo static::$test;
    }
}

$a = new A;
$b = new B;

echo '$a->test()<br />';
echo $a->test();
echo '<br /> <br />';
echo '$b->test()<br />';
echo $b->test();
echo '<br /> <br />';
echo '$b->static_test()<br />';
echo $b->static_test();

Safe to say I am stumped.

Was it helpful?

Solution

protected static $test = 'A TEST';
                 ^--- !!!

It's not a constant - so it should be preceded by $ sign

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top