3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Invalid example class Animal { public function eat(){} } class Cat extends Animal { public function meow(){ echo "meow"; } } class A { function foo(): Animal { return new Animal(); } } class B extends A { function foo(): Cat { return new Cat(); } } if (rand(0, 1)) { $object = new A; } else { $object = new B; } $animal = $object->foo(); $animal->meow(); // This code fails %50 of the time as not all animals can meow.
Output for 7.4.0 - 7.4.2, 7.4.4
Fatal error: Uncaught Error: Call to undefined method Animal::meow() in /in/WaOiP:36 Stack trace: #0 {main} thrown in /in/WaOiP on line 36
Process exited with code 255.
Output for 7.4.3
meow
Output for 7.2.0 - 7.2.29, 7.3.0 - 7.3.16
Fatal error: Declaration of B::foo(): Cat must be compatible with A::foo(): Animal in /in/WaOiP on line 25
Process exited with code 255.

preferences:
74.12 ms | 402 KiB | 57 Q