/
animal.php
92 lines (76 loc) · 2.13 KB
/
animal.php
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
class Animal
{
public $name;
public $health;
public function __construct($name)
{
$this->name = $name;
$this->health = 100;
}
public function walk()
{
$this->health -= 1;
echo 'walk, ';
return $this;
}
public function run()
{
$this->health -= 5;
echo 'run, ';
return $this;
}
public function displayHealth()
{
echo '<br /><strong>Animal Name:</strong> ' . $this->name . '<br />';
echo '<strong>Animal Health:</strong> ' . $this->health . '<br />';
echo '<br />';
return $this;
}
}
class Dog extends Animal
{
public function __construct($name)
{
parent::__construct($name);
$this->health = 150;
}
public function pet()
{
$this->health += 5;
echo 'pet, ';
return $this;
}
}
class Dragon extends Animal
{
public function __construct($name)
{
parent::__construct($name);
$this->health = 170;
}
public function fly()
{
$this->health -= 10;
echo 'fly, ';
return $this;
}
public function displayHealth()
{
echo '<br /><strong>Dragon Message: </strong><em>This is a dragon!</em>';
parent::displayHealth();
}
}
echo '<h2>$animal Object Instance of the Animal Class with Various Invoked Methods</h2>';
$animal = new Animal("animal");
$animal->walk()->walk()->walk()->run()->run()->displayHealth();
echo '<h2>$black_lab Object Instance of the Dog Subclass (of the Animal Parent Class) with Various Invoked Methods</h2>';
$black_lab = new Dog("Bo");
$black_lab->walk()->walk()->walk()->run()->run()->pet()->displayHealth();
echo '<h2>$crazy_dragon Object Instance of the Dragon Subclass (of the Animal Parent Class) with Various Invoked Methods</h2>';
$crazy_dragon = new Dragon("Crazy Dragon");
$crazy_dragon->walk()->walk()->walk()->run()->run()->fly()->fly()->displayHealth();
echo '<h2>Calling the fly() and pet() Methods on the Animal Class Will Display an Error Message Per the Code Below</h2>';
$lizard = new Animal("Freddy");
$lizard->pet()->fly();
?>