<?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();
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.