php

Inheritance in PHP

The three main features of object-oriented programming include Encapsulation, Inheritance, and Polymorphism. When programming, you may need to use certain code multiple times, and using inheritance reduces the repetition of rewriting code manually by reusing the code. Inheritance is a method for creating a new class by inheriting a base class. The object of the new class will be able to access all class members of the new class, as well as the base class, through inheritance. In this way, the same code can be reused many times by writing it only one time. PHP uses the extend keyword for inheritance. This tutorial shows how to implement inheritance using PHP script.

Syntax of Inheritance

The syntax of inheritance is shown below.

Class newClass extends oldClass
{
    ...
}

Here, newClass is called the child, derived, or subclass; and oldClass is called the parent, base, or superclass. The next part of this tutorial shows some examples of using inheritances in PHP.

Example 1: Simple Use of Inheritance

The following script shows a simple use of inheritance in PHP script. In the script, Employee is the parent class that contains two class variables, and the setData() method is used to initialize the class variables. The Executive class is the child class that is inherited from the Employee class using the extend keyword. It contains one class variable and a method named showDetails() to print the class variables of the parent and child classes. After creating the object of the child class, the methods of the parent and child classes will be called using the child class object.

<?php
//Parent class
class Employee{

//Parent class variables
public $name;
public $department;

//Initialize basic data
public function setData()
{
$this->name = "John Abraham";
$this->department = "HR";
}
}

//Child Class
class Executive extends Employee{

//Child class variable
public $designation = "Marketing Executive";

//Print employee details
public function showDetails()
{
if($this->name != "" && $this->designation != "" && $this->department != "")
{
echo "<b>Employee Details: </b><br/>";
echo "Name: ".$this->name."<br/>";
echo "Designation: ".$this->designation."<br/>";
echo "Department: ".$this->department."<br/>";
}
}
}

//Create object the child class
$objEmp=new Executive();
//Call parent class method
$objEmp->setData();
//Call child class method
$objEmp->showDetails();

?>

Output

The following output will appear after running the script. Here, the employee name and department values are printed from the parent class, and the employee designation value is printed from the child class.

Example 2: Inheritance with Method Overriding

Method overriding occurs when a method with the same name is declared in both the parent and the child class. The following script shows inheritance with method overriding using the PHP script. Here, the showDetails() method is declared in both the parent class and the child class. The object of the parent class will access the showDetails() method of the parent class, and the object of the child class will access the showDetails() method of the child class.

<?php
//Parent class
class Employee{

//Parent class variables
public $name;
public $department;

//Initialize data
function __construct()
{
$this->name = "Janifer Lopez";
$this->department = "Sales";
}
//Print employee details
public function showDetails()
{
echo "<b>Employee Details:[From Parent Class] </b><br/>";
echo "Name: ".$this->name."<br/>";
echo "Department: ".$this->department."<br/><br/>";
}
}

//Child Class
class Executive extends Employee{

//Child class variable
public $designation = "Sales Executive";

//Print employee details
public function showDetails()
{
echo "<b>Employee Details:[From Child Class] </b><br/>";
echo "Name: ".$this->name."<br/>";
echo "Designation: ".$this->designation."<br/>";
echo "Department: ".$this->department."<br/>";
}
}

//Create parent class object
$objEmployee=new Employee();
//Call parent class method
$objEmployee->showDetails();

//Create child class object
$objExecutive=new Executive();
//Call child class method
$objExecutive->showDetails();

?>

Output

The following output will appear after running the script. When the showDetails() method is called with the object of the parent class, it will show the output from the parent class. When the showDetails() method is called with the object of the child class, it will show the output from the child class.

Example 3: Call Parent Constructor Inside Child Constructor

When both the parent and the child class contain a constructor method, the child class can call the constructor of the parent class. The following script shows how to call the constructor of the parent class from the constructor of the child class. The parent::__construct() statement is used to call the parent constructor.

<?php
//Parent class
class Employee{

//Parent class variables
public $name;
public $department;

//Initialize data
function __construct($name,$dept)
{
$this->name = $name;
$this->department = $dept;
}
}

//Child Class
class Executive extends Employee{

//Child class variables
public $designation;
public $salary;

//Initialize data
function __construct($name,$department,$designation,$salary)
{
//Call parent class constructor
parent:: __construct($name,$department);
$this->designation = $designation;
$this->salary = $salary;
}

//Print employee details
public function showDetails()
{
echo "<b>Employee Details:</b><br/>";
echo "Name: ".$this->name."<br/>";
echo "Designation: ".$this->designation."<br/>";
echo "Department: ".$this->department."<br/>";
echo "Salary: $".$this->salary."<br/>";
}
}

//Create child class object
$objExecutive=new Executive('Jafar Iqbal','Marketing','Marketing Executive',4500);
//Call child class method
$objExecutive->showDetails();

?>

Output

The following output will appear after running the script. Here, the employee name and department are initialized by the parent constructor, and the employee designation and salary are initialized by the child constructor.

Example 4: Implement Hierarchical Inheritance

The following script shows how to implement hierarchical inheritance in PHP. Here, class2 is created by inheriting class1, and class3 is created by inheriting class2. In this example, three methods are defined in three classes. The class3 object is created to call the methods of all classes.

<?php
//Parent class
class class1{

function showMethod1()
{
echo "<font color='red' size='5px'>It is the parent class</font><br/>";
}
}

//Child Class
class class2 extends class1{

function showMethod2()
{
echo "<font color='green' size='4px'>It is the child class</font><br/>";
}
}

//Grand Child Class
class class3 extends class2{

function showMethod3()
{
echo "<font color='blue' size='2px'>It is the grand child class</font>";
}
}

$object = new class3();

$object->showMethod1();
$object->showMethod2();
$object->showMethod3();
?>

Output

The following output will appear after running the script. Here, the first line of text has appeared from class1, the second line of text has appeared from class2, and the third line of text has appeared from class3.

Video Tutorial

Conclusion

This tutorial showed you several different types of inheritances using PHP script. After reading this article, you should understand the concept of inheritance and its basic uses in PHP script.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.