/
poly.php
executable file
·222 lines (178 loc) · 3.97 KB
/
poly.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* defining the interface
*/
interface IAnimal
{
const CAN_FLY_MSG = "I'm flying high";
const CAN_NOT_FLY_MSG = "I can't fly";
public function tryToFly();
public function talk();
}
/**
* define the abstract class
*/
abstract class Animal implements IAnimal
{
private $animalName;
private $height;
private $weight;
private $favFood;
private $speed;
private $sound;
private $canFly;
public function __construct($animalName, $height, $weight, $favFood, $speed, $sound, $canFly)
{
$this->setanimalName($animalName);
$this->setheight($height);
$this->setweight($weight);
$this->setsound($sound);
$this->setspeed($speed);
$this->setfavFood($favFood);
$this->setcanFly($canFly);
}
public function tryToFly()
{
if ($this->getcanFly()) {
echo self::CAN_FLY_MSG;
} else {
echo self::CAN_NOT_FLY_MSG;
}
}
public function getanimalName()
{
return $this->animalName;
}
public function setanimalName($name)
{
$this->animalName = (string)$name;
}
public function getheight()
{
return $this->height;
}
public function setheight($height)
{
if (!(is_numeric($height) && $height > 0)) {
throw new Exception('The Height must be greater than Zero');
}
$this->height = (double)$height;
}
public function getweight()
{
return $this->weight;
}
public function setweight($weight)
{
if (!(is_numeric($weight) && $weight > 0)) {
throw new Exception('The Weight must be greater than Zero');
}
$this->weight = (double)$weight;
}
public function getfavFood()
{
return $this->favFood;
}
public function setfavFood($favFood)
{
$this->favFood = (string)$favFood;
}
public function getsound()
{
return $this->sound;
}
public function setsound($sound)
{
$this->sound = (string)$sound;
}
public function getspeed()
{
return $this->speed;
}
public function setspeed($speed)
{
$this->speed = (double)$speed;
}
public function getcanFly()
{
return $this->canFly;
}
public function setcanFly($canFly)
{
$this->canFly = (boolean)$canFly;
}
public function talk()
{
echo $this->getsound();
}
}
Class PlayWithAnimal
{
public function __construct(IAnimal $animal)
{
if ($animal instanceof Cat) {
$this->sayHello($animal);
}
if ($animal instanceof Bird) {
$this->sayHello($animal);
}
if ($animal instanceof Dog) {
$this->sayHello($animal);
echo "<br>";
echo $animal->getanimalName().' '. $animal->dig();
}
}
public function sayHello($animal)
{
echo "Hello ". $animal->getanimalName();
}
}
class Dog extends Animal
{
public function __construct($name, $height, $weight, $favFood, $speed, $sound, $canFly)
{
parent::__construct($name, $height, $weight, $favFood, $speed, $sound, $canFly);
}
public function dig()
{
echo "Dug a hole ";
}
}
class Cat extends Animal
{
public function __construct($name, $height, $weight, $favFood, $speed, $sound, $canFly)
{
parent::__construct($name, $height, $weight, $favFood, $speed, $sound, $canFly);
}
}
class Bird extends Animal
{
public function __construct($name, $height, $weight, $favFood, $speed, $sound, $canFly)
{
parent::__construct($name, $height, $weight, $favFood, $speed, $sound, $canFly);
}
}
try {
$dog = new Dog('dog', 3, 4, 'cat', 5, 'Bark', false);
$playwithanimal = new PlayWithAnimal($dog);
echo "<br>";
$dog->tryToFly();
echo "<br>";
$dog->talk();
echo "<br>";
$cat = new Cat('cat', 3, 4, 'mice', 5, 'miow', false);
$playwithanimal = new PlayWithAnimal($cat);
echo "<br>";
$cat->tryToFly();
echo "<br>";
$cat->talk();
echo "<br>";
$bird = new Bird('Bird', 3, 4, 'insect', 5, 'miow', true);
$playwithanimal = new PlayWithAnimal($bird);
echo "<br>";
$bird->tryToFly();
echo "<br>";
$bird->talk();
} catch(Exception $e) {
echo $e->getMessage();
}