Types of Inheritance in PHP
Like other programming languages such as C++, PHP does not support all types of inheritance directly through classes. PHP supports only three types of inheritance using classes. They are:
- Single inheritance
- Multilevel inheritance
- Hierarchical Inheritance
PHP does not support multiple inheritance or hybrid inheritance through classes because a class can extend only one parent class. However, PHP provides traits and interfaces, which allow you to achieve similar functionality by reusing methods from multiple sources. Using traits, PHP can simulate multiple and hybrid inheritance behavior effectively.
Let’s discuss all types of inheritance in PHP one by one in detail with the help of examples.
Single Inheritance in PHP
When a child class inherits from only one parent class at a time, it is called single inheritance in PHP. In other words, if a class extends only one parent class using extends keyword, it follows single inheritance. This is the most basic and commonly used type of inheritance in PHP applications.
In this type of inheritance, a child class can access the public and protected members (properties and methods) of its parent class. Single inheritance is simple, and easy to understand, making it widely used in PHP programming. Look at the below figure.
Basic Syntax
class ParentClass {
// Properties and methods
}
class ChildClass extends ParentClass {
// Additional or overridden properties and methods
}
In the above syntax,
- ParentClass represents the name of base class (also called parent class or superclass).
- ChildClass represents the name of derived class (also called child class or subclass).
- The child class automatically gets access to the public and protected members of the parent class, but it cannot access the parent’s private members directly.
Simple Example of Single Inheritance
<?php
class Animal {
public function eat() {
echo "Animal is eating. \n";
}
}
class Dog extends Animal {
public function bark() {
echo "Dog is barking. \n";
}
}
// Creating an object of class Animal.
$animal = new Animal();
// Accessing method of parent class.
$animal->eat();
// Creating an object of class Dog.
$dog = new Dog();
// Accessing inherited method of parent class and method defined in child class.
$dog->eat();
$dog->bark();
?>
Output:
Animal is eating. Animal is eating. Dog is barking.
In this example:
- Animal is the parent class, which contains a method eat(). This method prints “Animal is eating”.
- Dog is the child class that extends class Animal. This means that the Dog class inherits the method eat() of the parent class. It also defines its own method bark() which prints “Dog is barking”.
- An object $animal of class Animal is created, and calling eat() method prints “Animal is eating”.
- An object $dog of class Dog is created, which can access inherited method eat() from class Animal and method bark() from class Dog.
- The object $dog demonstrates inheritance as it can access both the inherited method and its own method.
- This is an example of single inheritance, where class Dog inherits from class Animal.
Advanced Example of Single Inheritance
<?php
class Employee {
protected $name;
protected $salary;
public function __construct($name, $salary) {
$this->name = $name;
$this->salary = $salary;
}
public function getDetails() {
return "Name: " . $this->name . ", Salary: " . $this->salary;
}
}
class Manager extends Employee {
private $department;
public function __construct($name, $salary, $department) {
parent::__construct($name, $salary);
$this->department = $department;
}
// Method overriding
public function getDetails() {
return parent::getDetails() . ", Department: " . $this->department;
}
}
// Creating an object of child class Manager.
$manager = new Manager("Rahul", 60000, "IT");
echo $manager->getDetails();
?>
Output:
Name: Rahul, Salary: 60000, Department: IT
In this example:
- Employee is the parent class, which contains the protected properties $name and $salary.
- This class has a constructor used to initialize the values of the variables $name and $salary.
- The Employee class defines a method getDetails(), which returns the employee’s name and salary.
- Manager is the child class that extends the Employee class and inherits its properties and methods.
- The parent::__construct() statement is used to call the constructor of the parent class.
- In the child class, the getDetails() method is overridden to add its own property $department.
- An object $manager is created from the child class, and it accesses both the inherited members and the overridden method.
Advantages of Single Inheritance
- Simple and Easy to Understand: Single inheritance has a straightforward structure, making it easy for beginners to learn and implement.
- Improves Code Reusability: The child class can reuse properties and methods of the parent class without rewriting code.
- Better Code Readability: Since only one parent class is involved, the class hierarchy remains clean and easy to follow.
- Easy Maintenance: Changes made in the parent class automatically reflect in the child class.
- Supports Method Overriding: Child classes can override parent methods to customize behavior or functionality.
Multilevel Inheritance in PHP
When a parent class is inherited by a child class and that child is further inherited by another child class, it is called multilevel inheritance in PHP.
In multilevel inheritance, a class inherits from a class, which in turn inherits from another class, creating a chain of inheritance. This structure creates a multi-level hierarchy where each child class inherits the properties and methods of its parent class.
Basic Syntax
class GrandParentClass {
// Properties and methods
}
class ParentClass extends GrandParentClass {
// Inherits from GrandParentClass
}
class ChildClass extends ParentClass {
// Inherits from ParentClass and indirectly from GrandParentClass
}
In the above syntax,
- GrandParentClass is the base or top-level class in the inheritance hierarchy.
- ParentClass extends GrandParentClass and inherits its public and protected members.
- ChildClass extends ParentClass and therefore gains access to the public and protected members of both parent classes.
Simple Example of Multilevel Inheritance
<?php
class Animal {
public function eat() {
echo "Animal is eating \n";
}
}
class Mammal extends Animal {
public function walk() {
echo "Mammal can walk \n";
}
}
class Dog extends Mammal {
public function bark() {
echo "Dog is barking \n";
}
}
$animal = new Animal();
$animal->eat();
$mammal = new Mammal();
$mammal->eat();
$mammal->walk();
$dog = new Dog();
$dog->eat();
$dog->walk();
$dog->bark();
?>
Output:
Animal is eating Animal is eating Mammal can walk Animal is eating Mammal can walk Dog is barking
In this example:
- Class Animal defines a method eat(), which prints “Animal is eating”.
- Class Mammal inherits from class Animal and adds a new method walk(), which prints “Mammal can walk”.
- Class Dog inherits from class Mammal and adds another method bark(), which prints “Dog is barking”.
- The object $animal can only access eat() method.
- The object $mammal can access eat() from class Animal and walk() from class Mammal.
- $dog object can access methods from both Animal and Mammal and also its own message() method.
- This is multilevel inheritance, where class Dog inherits from Mammal, and Mammal inherits from Animal.
Advanced Example of Multilevel Inheritance
<?php
class Person {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function getInfo() {
return "Name: " . $this->name;
}
}
class Employee extends Person {
protected $salary;
public function __construct($name, $salary) {
parent::__construct($name);
$this->salary = $salary;
}
public function getInfo() {
return parent::getInfo() . ", Salary: " . $this->salary;
}
}
class Manager extends Employee {
private $department;
public function __construct($name, $salary, $department) {
parent::__construct($name, $salary);
$this->department = $department;
}
public function getInfo() {
return parent::getInfo() . ", Department: " . $this->department;
}
}
$manager = new Manager("Amit", 75000, "IT");
echo $manager->getInfo();
?>Output:
Name: Amit, Salary: 75000, Department: IT
In this example:
- Person is the base class that contains a protected property $name.
- This class defines a constructor and a getInfo() method to return basic information.
- Employee is an intermediate class that inherits from the Person class and adds a protected property $salary.
- The parent::__construct() method is used to call the constructor of the parent class (Person).
- The Employee class overrides the getInfo() method to include salary information.
- Manager is the final child class that inherits from the Employee class and adds a private property $department.
- This class again overrides the getInfo() method and accesses methods and data inherited from all parent levels.
Advantages of Multilevel Inheritance
- Code Reusability: Common functionality can be reused across multiple levels of inheritance, reducing the need to write the same code again.
- Improves Code Organization: Each level of inheritance can focus on specific responsibilities, making the overall class structure more logical and easier to understand.
- Reduces Code Duplication: Shared logic is written once in higher-level classes and automatically reused by all descendant classes, which helps avoid repetition.
Hierarchical Inheritance in PHP
When a single parent class has more than one child class, it is called hierarchical inheritance in PHP. In other words, multiple child classes inheriting from a single parent class is called hierarchical inheritance.
Each child class can access the public and protected members of the common parent class, forming a hierarchical structure. Child classes can also define its own additional properties and methods. Hierarchical structure helps reduce code duplication and improves maintainability.
Basic Syntax
class ParentClass {
// Properties and methods
}
class ChildClass1 extends ParentClass {
// Inherits from ParentClass
}
class ChildClass2 extends ParentClass {
// Also inherits from ParentClass
}
In the above syntax,
- ParentClass is the common base class.
- ChildClass1 and ChildClass2 both extend the same parent class.
- Each child class can access the public and protected members of the parent class.
- Child classes are independent of each other.
Simple Example of Hierarchical Inheritance
<?php
class Shape {
public function draw() {
echo "Drawing a shape<br>";
}
}
class Circle extends Shape {
public function area() {
echo "Area of circle \n";
}
}
class Rectangle extends Shape {
public function area() {
echo "Area of rectangle \n";
}
}
$circle = new Circle();
$circle->draw();
$circle->area();
$rectangle = new Rectangle();
$rectangle->draw();
$rectangle->area();
?>
Output:
Drawing a shape Area of circle Drawing a shape Area of rectangle
In this example:
- Shape is the parent class.
- Circle and Rectangle are child classes.
- Both child classes inherit the draw() method.
- Each child class has its own implementation of the area() method.
Advanced Example of Hierarchical Inheritance
<?php
class Person {
protected $name;
protected $id;
public function __construct($name, $id) {
$this->name = $name;
$this->id = $id;
}
public function getBasicInfo() {
return "Name: {$this->name}, ID: {$this->id}";
}
}
class Student extends Person {
private $course;
public function __construct($name, $id, $course) {
parent::__construct($name, $id);
$this->course = $course;
}
public function getDetails() {
return parent::getBasicInfo() . ", Course: " . $this->course;
}
}
class Teacher extends Person {
private $subject;
public function __construct($name, $id, $subject) {
parent::__construct($name, $id);
$this->subject = $subject;
}
public function getDetails() {
return parent::getBasicInfo() . ", Subject: " . $this->subject;
}
}
class Staff extends Person {
private $department;
public function __construct($name, $id, $department) {
parent::__construct($name, $id);
$this->department = $department;
}
public function getDetails() {
return parent::getBasicInfo() . ", Department: " . $this->department;
}
}
$student = new Student("Deepak", "S101", "Computer Science");
$teacher = new Teacher("Dr. Saanvi", "T205", "Mathematics");
$staff = new Staff("Tripti", "ST301", "Administration");
echo $student->getDetails() . "\n";
echo $teacher->getDetails() . "\n";
echo $staff->getDetails();
?>
Output:
Name: Deepak, ID: S101, Course: Computer Science Name: Dr. Saanvi, ID: T205, Subject: Mathematics Name: Tripti, ID: ST301, Department: Administration
In this college example, hierarchical inheritance allows multiple classes such as Student, Teacher, and Staff to inherit common features from a single parent class (Person). Each child class extends the parent with its own unique properties and methods, making the design clean, reusable, and easy to maintain.
Advantages of Hierarchical Inheritance
- Code Reusability: Common functionality defined in the parent class can be reused by multiple child classes.
- Improves Maintainability: Any change in the parent class automatically affects all child classes.
- Supports Polymorphism: Different child classes can override methods of parent class and provide their own implementations.
Conclusion
Inheritance is one of the core pillars of object-oriented programming (OOP) in PHP. Understanding the types of inheritance in PHP helps you write clean, reusable, and maintainable code. PHP supports Single, Multilevel, and Hierarchical inheritance. However, you can achieve the same functionality of multiple and hybrid inheritance using Traits.
Stay tuned with the next tutorial where we will learn about Traits in PHP and implement multiple and hybrid inheritance using it.






