Project

General

Profile

Actions

Object Oriented Programming » History » Revision 1

Revision 1/21 | Next »
Amber Herold, 08/02/2011 12:14 PM


Object Oriented Programming

Polymorhism

PHP example

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 'Woof! Woof!';
    }
}

$animals = array(
    new Cat('Missy'),
    new Cat('Mr. Mistoffelees'),
    new Dog('Lassie')
);

foreach ($animals as $animal) {
    echo $animal->getName() . ': ' . $animal->talk();
}

Updated by Amber Herold over 13 years ago · 1 revisions