fork download
  1. <?php
  2.  
  3. class A {
  4. public function test() {
  5. echo self::who(); // always class A
  6. echo static::who(); // always the current class (static context)
  7. echo $this->who(); // always the current class (object context)
  8. }
  9. public function who() {
  10. echo __CLASS__ . "\n";
  11. }
  12. }
  13.  
  14. class B extends A {
  15. public function who() {
  16. echo __CLASS__ . "\n";
  17. }
  18. }
  19.  
  20. class C extends B {
  21. public function who() {
  22. echo __CLASS__ . "\n";
  23. }
  24. }
  25.  
  26. (new B)->test();
  27. (new C)->test();
  28. C::test();
  29.  
Runtime error #stdin #stdout #stderr 0.02s 23744KB
stdin
Standard input is empty
stdout
A
B
B
A
C
C
A
C
stderr
PHP Fatal error:  Uncaught Error: Using $this when not in object context in /home/uHOHLI/prog.php:7
Stack trace:
#0 /home/uHOHLI/prog.php(28): A::test()
#1 {main}
  thrown in /home/uHOHLI/prog.php on line 7