polymorphism in php

by gowtham 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]

Tagged in:

990
like
0
dislike
0
mail
flag

You must LOGIN to add comments