Example of inheritance in PHP


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
Output
Addition=1500
subtraction=500
multiplication=500000

Eg ii
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
Output
Name = Amit Age = 26 Email = kinje@example.com Phone = 1234567890

Comments

Popular posts from this blog

How to Install Squirrel Mail in ubuntu

How to Display Date in PHP

Export HTML Table Data to Excel, CSV, PNG and PDF using jQuery Plugin