-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path38 OOP Inheritance.php
76 lines (67 loc) · 1.69 KB
/
38 OOP Inheritance.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
<!-- PHP OOP - Inheritance -->
<title>PHP OOP - Inheritance</title>
<?php
// Define A Class
class Animal
{
// Properties
public $name;
protected $color;
private $leg;
// Method
protected function intro()
{
echo "The Animal/Bird Name is : $this->name and the Color is $this->color <br>";
}
// Constructor Method
function __construct($animalName, $animalColor, $animalLeg)
{
$this->name = $animalName;
$this->color = $animalColor;
$this->leg = $animalLeg;
$this->intro();
}
// Destructor Method
function __destruct()
{
echo "$this->name - $this->color - $this->leg <br>";
}
}
// Bird is Inherited from Animal
class Bird extends Animal
{
// Properties
public $feather;
// Method
function message()
{
echo "Am I a Animal or Bird?<br>";
// Call protected method from within derived class - OK
$this->intro();
}
// Constructor Method
function __construct($birdName, $birdColor, $birdLeg, $birdFeather)
{
$this->name = $birdName;
$this->color = $birdColor;
$this->leg = $birdLeg;
$this->feather = $birdFeather;
}
// Destructor Method
function __destruct()
{
echo "$this->name - $this->color - $this->leg - $this->feather <br>";
}
}
echo "<hr><b>Constructor : - </b><br>";
// Define An Object
$dog = new Animal('Dog', 'Brown', 4);
$ox = new Animal('Ox', 'White', 4);
echo "<hr><b>Object Method : - </b><br>";
// Define An Object
$parrot = new Bird('Parrot', 'Green', 2, 2);
$parrot->message();
$pigeon = new Bird('Pigeon', 'Blue', 2, 2);
$pigeon->message();
echo "<hr><b>Destructor : - </b><br>";
?>