<?php
abstract class Animal
{
protected $name;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
abstract public function sound();
}
class Cat extends Animal
{
public function __construct($name)
{
$this->name = $name;
}
public function sound()
{
echo $this->name . ' sound meow<br>';
}
}
class Dog extends Animal
{
public function __construct($name)
{
$this->name = $name;
}
public function sound()
{
echo $this->name . ' sound woof<br>';
}
}
class Horse extends Animal
{
public function __construct($name)
{
$this->name = $name;
}
public function sound()
{
echo $this->name . ' sound neigh<br>';
}
}
$cat1 = new Cat('Cat 1');
$cat2 = new Cat('Cat 2');
$dog1 = new Dog('Dog 1');
$dog2 = new Dog('Dog 2');
$horse1 = new Horse('Horse 1');
$horse2 = new Horse('Horse 2');
$animals = [$cat1, $dog1, $horse1, $cat2, $dog2, $horse2];
foreach ($animals as $animal) {
$animal->sound();
}
?>
Cat 1 sound meow
Dog 1 sound woof
Horse 1 sound neigh
Cat 2 sound meow
Dog 2 sound woof
Horse 2 sound neigh