-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.php
More file actions
59 lines (53 loc) · 1.5 KB
/
index.php
File metadata and controls
59 lines (53 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>PHP OOP Example</title>
</head>
<body>
<?php
final class Index {
public function init() {
spl_autoload_register(array($this, 'loadClass'));
}
public function loadClass($name) {
$classes = array(
'AnimalInterface' => 'model/AnimalInterface.php',
'Animal' => 'model/Animal.php',
'Cat' => 'model/Cat.php',
'Fish' => 'model/Fish.php'
);
if (!array_key_exists($name, $classes)) {
die('Class "' . $name . '" not found.');
}
require_once $classes[$name];
}
public function run() {
$cat = new Cat();
//breathe() is a polymorphic method, so we can use the
//respirate($animal)method instead of the following line
//echo "<br/>".$cat->breathe()."<br/>";
self::respirate($cat);
echo $cat->move()."<br/>";
$fish = new Fish();
//breathe() is a polymorphic method, so we can use the
//respirate($animal)method instead of the following line
//echo "<br/>".$fish->breathe()."<br/>";
self::respirate($fish);
echo $fish->move()."<br/>";
}
/**
* This function makes use of polymorphism. Both subtypes of
* Animal implement breathe(), so either can be passed in.
* @param type $animal
*/
private function respirate($animal){
echo $animal->breathe()."<br/>";
}
}
$index = new Index();
$index->init();
$index->run();
?>
</body>
</html>