Two classes extends
Animal
abstract class and both implement the same method
prey()
differently
abstract class Animal {
    // child classes must implement this
    abstract function prey();

    public function run() {
        echo "I am running!\n";
    }
}

class Dog extends Animal {
    public function prey() {
        echo "I killed the cat !\n";
    }
}

class Cat extends Animal {
    public function prey() {
        echo "I killed the rat !\n";
    }
}

$dog = new Dog();
$cat = new Cat();

$dog->prey(); // I killed the cat !
$cat->prey(); // I killed the rat !

$dog->run(); // I am running!
$cat->run(); // I am running!
by Valeri Tandilashvili
4 years ago
PHP
OOP
2
Pro tip: use ```triple backticks around text``` to write in code fences