<?php
class Foo
{
const NAME = 'Foo';
//Always Foo::NAME (Foo) due to self
protected static $staticName = self::NAME;
public function __construct()
{
echo $this::NAME;
}
public function getStaticName()
{
echo $this::$staticName;
}
}
class Bar extends Foo
{
const NAME = 'FooBar';
/**
* override getStaticName to output Bar::NAME
*/
public function getStaticName()
{
$this::$staticName = $this::NAME;
parent::getStaticName();
}
}
$foo = new Foo; //outputs Foo
echo PHP_EOL;
$bar = new Bar; //outputs FooBar
echo PHP_EOL;
$foo->getStaticName(); //outputs Foo
echo PHP_EOL;
$bar->getStaticName(); //outputs FooBar
echo PHP_EOL;
$foo->getStaticName(); //outputs FooBar
PD9waHAKCmNsYXNzIEZvbwp7CiAgICBjb25zdCBOQU1FID0gJ0Zvbyc7CgogICAgLy9BbHdheXMgRm9vOjpOQU1FIChGb28pIGR1ZSB0byBzZWxmCiAgICBwcm90ZWN0ZWQgc3RhdGljICRzdGF0aWNOYW1lID0gc2VsZjo6TkFNRTsKCiAgICBwdWJsaWMgZnVuY3Rpb24gX19jb25zdHJ1Y3QoKQogICAgewogICAgICAgIGVjaG8gJHRoaXM6Ok5BTUU7CiAgICB9CgogICAgcHVibGljIGZ1bmN0aW9uIGdldFN0YXRpY05hbWUoKQogICAgewogICAgICAgZWNobyAkdGhpczo6JHN0YXRpY05hbWU7CiAgICB9Cn0KCmNsYXNzIEJhciBleHRlbmRzIEZvbwp7CiAgICBjb25zdCBOQU1FID0gJ0Zvb0Jhcic7CgogICAgLyoqCiAgICAgKiBvdmVycmlkZSBnZXRTdGF0aWNOYW1lIHRvIG91dHB1dCBCYXI6Ok5BTUUKICAgICAqLwogICAgcHVibGljIGZ1bmN0aW9uIGdldFN0YXRpY05hbWUoKQogICAgewogICAgICAgICR0aGlzOjokc3RhdGljTmFtZSA9ICR0aGlzOjpOQU1FOwogICAgICAgIHBhcmVudDo6Z2V0U3RhdGljTmFtZSgpOwogICAgfQp9CgokZm9vID0gbmV3IEZvbzsgLy9vdXRwdXRzIEZvbwplY2hvIFBIUF9FT0w7CiRiYXIgPSBuZXcgQmFyOyAvL291dHB1dHMgRm9vQmFyCmVjaG8gUEhQX0VPTDsKJGZvby0+Z2V0U3RhdGljTmFtZSgpOyAvL291dHB1dHMgRm9vCmVjaG8gUEhQX0VPTDsKJGJhci0+Z2V0U3RhdGljTmFtZSgpOyAvL291dHB1dHMgRm9vQmFyCmVjaG8gUEhQX0VPTDsKJGZvby0+Z2V0U3RhdGljTmFtZSgpOyAvL291dHB1dHMgRm9vQmFy