fork(1) download
  1. <?php
  2.  
  3. class Foo
  4. {
  5. const NAME = 'Foo';
  6.  
  7. //Always Foo::NAME (Foo) due to self
  8. protected static $staticName = self::NAME;
  9.  
  10. public function __construct()
  11. {
  12. echo $this::NAME;
  13. }
  14.  
  15. public function getStaticName()
  16. {
  17. echo $this::$staticName;
  18. }
  19. }
  20.  
  21. class Bar extends Foo
  22. {
  23. const NAME = 'FooBar';
  24.  
  25. /**
  26.   * override getStaticName to output Bar::NAME
  27.   */
  28. public function getStaticName()
  29. {
  30. $this::$staticName = $this::NAME;
  31. parent::getStaticName();
  32. }
  33. }
  34.  
  35. $foo = new Foo; //outputs Foo
  36. echo PHP_EOL;
  37. $bar = new Bar; //outputs FooBar
  38. echo PHP_EOL;
  39. $foo->getStaticName(); //outputs Foo
  40. echo PHP_EOL;
  41. $bar->getStaticName(); //outputs FooBar
  42. echo PHP_EOL;
  43. $foo->getStaticName(); //outputs FooBar
Success #stdin #stdout 0.04s 52480KB
stdin
Standard input is empty
stdout
Foo
FooBar
Foo
FooBar
FooBar