I can’t seem to be able to override protected static variables. It’s rather annoying, given that you cannot override any private variable as well. Howcan I fix that? (Have to support PHP 5.2)
<?
class Foo{
protected static $stuff = 'Foo';
public function showStuff(){
echo self::$stuff . PHP_EOL;
}
}
class Bar extends Foo{
protected static $stuff = 'Bar';
}
$f = new Foo();
$b = new Bar();
$f->showStuff(); // Output: Foo
$b->showStuff(); // Output: Foo
?>
You need to use late static bindings, a feature introduced in PHP 5.3. In your class
Foo,selfrefers to theFooclass. You want to refer to the class where the call originated. You need to use the keywordstatic: