Inheritance is a mechanism of extending an existing class by inheriting a class we create a new class with all functionality of that existing class, and we can add new members to the new class.
When we inherit one class from another we say that inherited class is a subclass and the class who has inherit is called parent class.
We declare a new class with additional keyword extends.
Note : PHP only supports multilevel inheritance.
Eg i
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
|
<?php
class BaseClass
{
function add()
{
$x=1000;
$y=500;
$add=$x+$y;
echo "Addition=".$add."<br/>";
}
}
class chld extends BaseClass
{
function sub()
{
$x=1000;
$y=500;
$sub=$x-$y;
echo "subtraction=".$sub."<br/>";
}
}
class Nestedchld extends chld
{
function mult()
{
$x=1000;
$y=500;
$mult=$x*$y;
echo "multiplication=".$mult."<br/>";
}
}
class show extends Nestedchld
{
function __construct()
{
parent::add();
parent::sub();
parent::mult();
}
}
$obj= new show();
?>
|
Output
Addition=1500
subtraction=500
multiplication=500000
Eg ii
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
|
<?php
class person
{
var $name;
var $address;
var $phone;
function printPersonInf()
{
echo $this->name;
echo $this->address;
echo $this->phone;
}
}
class employee extends person
{
var $occupation;
var $com_name;
var $buss_phone;
function printempInfo()
{
parent:: printPersonInf();
echo $this->occupation;
echo $this->com_name;
echo $this->buss_phone;
}
}
$obj= new person();
$emp=new employee();
echo $emp->name="kinje"."<br/>";
echo $emp->address="Songea"."<br/>";
echo $emp->phone="0673101032."<br/>";
echo $emp->occupation="developer"."<br/>";
echo $emp->comp_name="hope code ltd"."<br/>";
echo $emp->buss_phone="0673101032"."<br/>";
?>
|
Output
kinje
songea
0673101032
developer
hope code ltd
0673101032
In the above example
A class person have defined with three properties $name,$address and $phone, with a method printPersonInf( )
Another class employee which is child class of person have also defined with three properties $occupation,$com_name,$buss_phone with a method printempInfo( ).
It means all the property of person class can accessible into employee class also so why
printPersoninfo( ) method is accessible here in printempInfo( ) method.
Now create the object of class employee and access all the property of its own and its inherited class.
Eg iii
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
|
<?php
class CommonParam
{
var $name;
var $age;
function commonInfo($name,$age)
{
echo 'Name = '.$this->name=$name;
echo '
Age = '.$this->age=$age;
}
}
class EmpParam extends CommonParam
{
var $email;
var $phone;
function empInfo($x,$y,$p,$q)
{
parent::commonInfo($x,$y);
echo '
Email = '.$this->email=$p;
echo '
Phone = '.$this->phone=$q;
}
}
$objemp=new EmpParam();
//will pass the parameter valuewhile creating object
$objemp->empInfo('Amit',26,'kinje@example.com',1234567890);
?>
|
Output
Name = Amit Age = 26 Email = kinje@example.com Phone = 1234567890
Comments
Post a Comment