3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Food { public function consume() { } } class AnimalFood extends Food { } abstract class Animal { protected string $name; public function __construct(string $name) { $this->name = $name; } abstract public function speak(); 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 AnimalShelter { public static function adopt(string $name): Animal; } class CatShelter implements AnimalShelter { public static function adopt(string $name): Cat { return new Cat($name); } } class DogShelter implements AnimalShelter { public static function adopt(string $name): Dog { return new Dog($name); } } $kitty = CatShelter::adopt("Ricky"); $kitty->speak();
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
meow
Output for 7.1.26 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/GvK1N on line 15
Process exited with code 255.

preferences:
166.78 ms | 402 KiB | 181 Q