Project

General

Profile

Object Oriented Programming » History » Version 1

Amber Herold, 08/02/2011 12:14 PM

1 1 Amber Herold
h1. Object Oriented Programming
2
3
4
h2. Polymorhism
5
6
h3. PHP example
7
8
<pre>
9
interface IAnimal {
10
    function getName();
11
    function talk();
12
}
13
 
14
abstract class AnimalBase implements IAnimal {
15
    protected $name;
16
 
17
    public function __construct($name) {
18
        $this->name = $name;
19
    }
20
 
21
    public function getName() {
22
        return $this->name;
23
    }
24
}
25
 
26
class Cat extends AnimalBase {
27
    public function talk() {
28
        return 'Meowww!';
29
    }
30
}
31
 
32
class Dog extends AnimalBase {
33
    public function talk() {
34
        return 'Woof! Woof!';
35
    }
36
}
37
 
38
$animals = array(
39
    new Cat('Missy'),
40
    new Cat('Mr. Mistoffelees'),
41
    new Dog('Lassie')
42
);
43
 
44
foreach ($animals as $animal) {
45
    echo $animal->getName() . ': ' . $animal->talk();
46
}
47
</pre>