<?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 $this->name . " barks";
}
/*
* 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 $this->name . " meows";
}
}
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();
echo "\n";
$doggy = DogShelter::adopt("Mavrick");
$doggy->speak();
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Ricky meows
Mavrick barks
Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/rgPP2 on line 15
Process exited with code 255.