Dev Diaries WD logo Dev Diaries WD
PHP OOP · EP 14
PHP OOP · Episode 14

OOP: Polymorphism (one call, many forms)

The same method — different behaviour per object

ep14_oop_polymorphism.php
<?php
interface Animals{
   public function speak();
}

class Dog implements Animals{
  public function speak(){
    return "The Dog Barks";
  }
}

class Cat implements Animals{
  public function speak(){
     return "The Cat Meows";
  }
}

class Bird implements Animals{
  public function speak(){
     return "The Bird Chirps";
  }
}

$animals = [new Dog(), new Cat(), new Bird()];

foreach ($animals as $animal) {
    echo $animal->speak();
}
Output
Output for this tutorial goes here.

The Dog Barks

The Cat Meows

The Bird Chirps