3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Food { public function consume() { } } class AnimalFood extends Food { } abstract class Animal { protected $name; public function __construct(string $name) { $this->name = $name; } public function getName(): string { return $this->name; } abstract public function talk(); public function eat(AnimalFood $food) { $food->consume(); } } class Dog extends Animal { public function speak() { echo "woof"; } /* * The dog doesn't care if it's animal food, it'll eat ANY food */ public function eat(Food $food) { $food->consume(); } } class Cat extends Animal { public function speak() { echo "meow"; } } interface Petshop { public function buy(string $name): Animal; } class CatPetshop implements Petshop { public function buy(string $name): Cat { return new Cat($name); } } class DogPetshop implements Petshop { public function buy(string $name): Cat { return new Dog($name); } } $shop = some_func_returning_petshop(); $shop->buy('mavrick')->talk();
Output for 7.4.0
Fatal error: Class Dog contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Animal::talk) in /in/PpmWd on line 43
Process exited with code 255.
Output for 7.1.26 - 7.1.33, 7.2.17 - 7.2.25, 7.3.0 - 7.3.12
Warning: Declaration of Dog::eat(Food $food) should be compatible with Animal::eat(AnimalFood $food) in /in/PpmWd on line 43 Fatal error: Class Dog contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Animal::talk) in /in/PpmWd on line 43
Process exited with code 255.

preferences:
178 ms | 1395 KiB | 38 Q