Home >>PHP Object Oriented >PHP Inheritance

PHP Inheritance

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

<?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

<?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="shashi"."<br/>";

echo $emp->address="new delhi"."<br/>";

echo $emp->phone="06275-258542"."<br/>";

echo $emp->occupation="developer"."<br/>";

echo $emp->comp_name="Arise tech pvt ltd"."<br/>";

echo $emp->buss_phone="9015501123"."<br/>";
 
?>
Output shashi new delhi 06275-258542 developer Arise tech pvt ltd 9015501123

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

<?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,'amittest@test.com',1234567890);

?>
Output Name = Amit Age = 26 Email = amittest@test.com Phone = 1234567890

No Sidebar ads