polymorphism in php
by gowtham[ Edit ] 2010-02-15 10:19:00
interface IAnimal
{
function getName();
function talk();
}
abstract class AnimalBase implements IAnimal
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
class Cat extends AnimalBase
{
public function talk()
{
return "Meowww!";
}
}
class Dog extends AnimalBase
{
public function talk()
{
return "Arf! Arf!";
}
}
$animals = array(
new Cat("Missy"),
new Cat("Mr. Mistoffelees"),
new Dog("Lassie")
);
foreach ($animals as $animal) {
echo $animal->getName() . ": " . $animal->talk();
}
?>
[edit]