Results 1 to 3 of 3
Apr 20, 2009, 23:58 #1
- Join Date
- Feb 2005
- Beyond the seas there is a town
- 0 Post(s)
- 0 Thread(s)
// This is the base animal class, which Cat and Dog derive from
$this->name = $name;
class Cat extends Animal
// Cat inherits $name, setName() and makeSound() from Animal.
// Here we override makeSound()
class Dog extends Animal
class Squirrel extends Animal
// Since we don't define anything here, Squirrel will just
inherit everything from Animal and leave it at that
$a = new Animal;
$c = new Cat;
$d = new Dog;
$s = new Squirrel;
$a->makeSound(); // Outputs "..."
$c->makeSound(); // Outputs "Meow!"
$d->makeSound(); // Outputs "Woof!"
$s->makeSound(); // Outputs "..."
$animals = array($c, $s, $d, $a);
foreach($animals as $animal)
$animal->makeSound(); // Outputs "Meow!", "...", "Woof!" and
I shall build a boat,I shall cast it in the water,
I shall sail away from this strange earth,
Where no one awaken the heroes in the wood of love
Apr 21, 2009, 00:47 #2
Do you have a question?Guido - Community Team Leader
The Votes Are In: The Winners of the 2013 Community Awards are...
Blog - Free Flash Slideshow Widget
Apr 21, 2009, 00:49 #3
- Join Date
- Oct 2006
- France, deep rural.
- 17 Post(s)
- 1 Thread(s)
Next step is to make sure Animal cannot be called, only implemented. Someone already converted the Head First Design Pattern example of a Strategy Pattern (featuring animals, btw) to PHP, it may expand your horizon.