Introduction
Inheritance is an important concept in PHP Object-Oriented Programming (OOP). It allows one class to reuse the properties and methods of another class. For example, a Student class can inherit common information from a Person class. In this chapter, you will learn how inheritance works through practical PHP examples.
Understanding Inheritance in PHP
Inheritance allows a child class to reuse the properties and methods of a parent class.
Instead of writing the same code again in multiple classes, you can write common code once in a parent class and reuse it in child classes.
1. Parent Class and Child Class
- Parent Class: The class whose properties and methods are inherited.
- Child Class: The class that inherits properties and methods from another class.
PHP uses the extends keyword to create inheritance.
Syntax:
class ParentClass
{
// Properties and methods
}
class ChildClass extends ParentClass
{
// Additional properties and methods
}
2. Simple Inheritance Example
<?php
class Person
{
public $name = "Rahul";
public function displayName()
{
echo "Name: " . $this->name;
}
}
class Student extends Person
{
}
$student = new Student();
$student->displayName();
?>
Output:
Name: Rahul
Explanation:
Personis the parent class.Studentis the child class.- The
extendskeyword connects the two classes. - The
Studentclass inherits thenameproperty anddisplayName()method. - The child object can use the inherited method without writing it again.
3. Types of Inheritance in PHP
PHP supports inheritance through classes. The common inheritance structures are:
| Inheritance Type | Meaning |
|---|---|
| Single Inheritance | One child class inherits from one parent class. |
| Multilevel Inheritance | A class inherits from a child class, forming a chain. |
| Hierarchical Inheritance | Multiple child classes inherit from the same parent class. |
| Multiple Inheritance | One class directly inherits from multiple classes. |
Important: PHP does not support multiple inheritance through classes. However, similar code-reuse requirements can be handled using traits and interfaces.
Question 1: Create Simple Inheritance in PHP
Problem Statement
Create a parent class named Person with a property called name and a method called displayName(). Create a child class named Student that inherits from Person. Display the student’s name using the child object.
PHP Solution
<?php
class Person
{
public $name = "Rahul";
public function displayName()
{
echo "Person Name: " . $this->name;
}
}
class Student extends Person
{
}
$student = new Student();
$student->displayName();
?>
Sample Output
Person Name: Rahul
Explanation
Personis the parent class.- The
Personclass contains a public property and a public method. Student extends PersonmakesStudentthe child class.- The child class automatically inherits the accessible members of its parent.
- The
$studentobject can calldisplayName()even though the method is written insidePerson.
This is called single inheritance because one child class inherits from one parent class.
Concepts Covered
- Inheritance in PHP
- Parent class
- Child class
extendskeyword- Reusing inherited methods
Question 2: Access Parent Class Properties in a Child Class
Problem Statement
Create an Employee parent class with properties name and department. Create a child class named Manager that inherits these properties and displays employee details.
PHP Solution
<?php
class Employee
{
public $name = "Amit";
public $department = "IT";
}
class Manager extends Employee
{
public function displayDetails()
{
echo "Employee Name: " . $this->name . "<br>";
echo "Department: " . $this->department;
}
}
$manager = new Manager();
$manager->displayDetails();
?>
Sample Output
Employee Name: Amit
Department: IT
Explanation
- The
Employeeclass contains two public properties. - The
Managerclass inherits fromEmployee. - The child class accesses the inherited properties using
$this. - There is no need to declare the same properties again inside
Manager.
The $this keyword refers to the current object, including its inherited properties.
Concepts Covered
- Inheriting properties
- Accessing parent class properties
$thiskeyword- Code reusability
Question 3: Access Parent Class Methods in a Child Class
Problem Statement
Create a parent class named Animal with a method called eat(). Create a child class named Dog that inherits the method and adds its own method called bark().
PHP Solution
<?php
class Animal
{
public function eat()
{
echo "Animal is eating.<br>";
}
}
class Dog extends Animal
{
public function bark()
{
echo "Dog is barking.";
}
}
$dog = new Dog();
$dog->eat();
$dog->bark();
?>
Sample Output
Animal is eating.
Dog is barking.
Explanation
Animalis the parent class.Doginherits theeat()method fromAnimal.- The
Dogclass also contains its ownbark()method. - The child object can call both inherited and its own methods.
Inheritance allows child classes to use existing functionality while adding new functionality.
Concepts Covered
- Inheriting parent methods
- Creating child class methods
- Reusing code
- Single inheritance
Question 4: Create Multilevel Inheritance in PHP
Problem Statement
Create three classes: Person, Employee and Manager.
Personshould contain a property calledname.Employeeshould inherit fromPersonand contain a property calledsalary.Managershould inherit fromEmployeeand display both properties.
PHP Solution
<?php
class Person
{
public $name = "Rahul";
}
class Employee extends Person
{
public $salary = 40000;
}
class Manager extends Employee
{
public function displayDetails()
{
echo "Employee Name: " . $this->name . "<br>";
echo "Salary: Rs. " . $this->salary;
}
}
$manager = new Manager();
$manager->displayDetails();
?>
Sample Output
Employee Name: Rahul
Salary: Rs. 40000
Explanation
Personis the first parent class.Employeeinherits fromPerson.Managerinherits fromEmployee.- Because of this inheritance chain,
Managercan access the public properties from both earlier classes. - This structure is called multilevel inheritance.
Inheritance Chain:
Person → Employee → Manager
Concepts Covered
- Multilevel inheritance
- Multiple inheritance levels
- Parent and child relationships
- Inheriting properties across classes
Question 5: Create Hierarchical Inheritance in PHP
Problem Statement
Create a parent class named Vehicle with a property called brand and a method called displayBrand().
Create two child classes:
CarBike
Both child classes should inherit from Vehicle and display the brand with their own vehicle type.
PHP Solution
<?php
class Vehicle
{
public $brand = "Honda";
public function displayBrand()
{
echo "Brand: " . $this->brand . "<br>";
}
}
class Car extends Vehicle
{
public function displayCar()
{
$this->displayBrand();
echo "Vehicle Type: Car<br>";
}
}
class Bike extends Vehicle
{
public function displayBike()
{
$this->displayBrand();
echo "Vehicle Type: Bike<br>";
}
}
$car = new Car();
$bike = new Bike();
$car->displayCar();
echo "<br>";
$bike->displayBike();
?>
Sample Output
Brand: Honda
Vehicle Type: Car
Brand: Honda
Vehicle Type: Bike
Explanation
Vehicleis the parent class.CarandBikeare two separate child classes.- Both child classes inherit the
brandproperty anddisplayBrand()method. - Each child class contains its own additional method.
- Multiple child classes inheriting from the same parent class create hierarchical inheritance.
Concepts Covered
- Hierarchical inheritance
- Multiple child classes
- Sharing parent class properties
- Sharing parent class methods
Question 6: Use the parent Keyword in PHP
Problem Statement
Create a parent class named Person with a method called displayName(). Create a child class named Student with its own displayDetails() method.
Use the parent keyword to call the parent class method from the child class.
PHP Solution
<?php
class Person
{
public function displayName()
{
echo "Name: Rahul<br>";
}
}
class Student extends Person
{
public function displayDetails()
{
parent::displayName();
echo "Course: PHP";
}
}
$student = new Student();
$student->displayDetails();
?>
Sample Output
Name: Rahul
Course: PHP
Explanation
Personis the parent class.Studentinherits fromPerson.- The
parent::displayName()statement calls the method defined in the parent class. - The child class then displays its own course information.
- The
parentkeyword is useful when you want to reuse parent class functionality explicitly.
Important: parent:: is used to access parent class methods and constants, not ordinary object properties.
Concepts Covered
parentkeyword- Calling parent class methods
- Inheritance
- Reusing parent functionality
Question 7: Override a Parent Class Method
Problem Statement
Create a parent class named Animal with a method called sound() that displays a general animal sound.
Create a child class named Dog and override the sound() method to display a dog-specific sound.
PHP Solution
<?php
class Animal
{
public function sound()
{
echo "Animal makes a sound.";
}
}
class Dog extends Animal
{
public function sound()
{
echo "Dog says: Woof Woof!";
}
}
$animal = new Animal();
$dog = new Dog();
echo "Animal: ";
$animal->sound();
echo "<br>Dog: ";
$dog->sound();
?>
Sample Output
Animal: Animal makes a sound.
Dog: Dog says: Woof Woof!
Explanation
- The
Animalclass contains a method calledsound(). - The
Dogclass inherits fromAnimal. - The child class defines another method with the same name.
- The child class method replaces the inherited method’s behavior when called on a
Dogobject. - This process is called method overriding.
Method overriding allows a child class to provide its own implementation of an inherited method.
Concepts Covered
- Method overriding
- Parent and child methods
- Inheritance
- Different method behavior
Question 8: Override a Parent Method and Call It Using parent
Problem Statement
Create a parent class named Employee with a method called displayDetails() that displays the employee’s name.
Create a child class named Manager that overrides the same method. Use parent::displayDetails() to display the parent’s information and then display the manager’s department.
PHP Solution
<?php
class Employee
{
public function displayDetails()
{
echo "Employee Name: Amit<br>";
}
}
class Manager extends Employee
{
public function displayDetails()
{
parent::displayDetails();
echo "Department: Management";
}
}
$manager = new Manager();
$manager->displayDetails();
?>
Sample Output
Employee Name: Amit
Department: Management
Explanation
- The parent class contains the
displayDetails()method. - The child class defines the same method, so it overrides the inherited method.
parent::displayDetails()executes the original parent class method.- The child class then displays its additional information.
- This approach allows you to extend parent functionality instead of completely replacing it.
Concepts Covered
- Method overriding
parentkeyword- Reusing overridden parent methods
- Extending inherited functionality
Question 9: Use Protected Properties in Inheritance
Problem Statement
Create a parent class named Employee with protected properties name and salary.
Create a child class named Manager that inherits these properties and displays employee information through a public method.
PHP Solution
<?php
class Employee
{
protected $name = "Priya";
protected $salary = 55000;
}
class Manager extends Employee
{
public function displayDetails()
{
echo "Employee Name: " . $this->name . "<br>";
echo "Salary: Rs. " . $this->salary;
}
}
$manager = new Manager();
$manager->displayDetails();
?>
Sample Output
Employee Name: Priya
Salary: Rs. 55000
Explanation
- The parent class contains two protected properties.
- The child class inherits from the parent class.
- Protected properties can be accessed inside child classes.
- The
displayDetails()method uses$thisto access inherited properties. - The properties cannot be accessed directly from outside the class.
This example demonstrates how protected access modifiers support inheritance while keeping properties hidden from external code.
Concepts Covered
- Protected properties
- Inheritance with protected members
- Access control
- Parent and child classes
Question 10: Build an Employee Management System Using Inheritance
Problem Statement
Create an employee management system using inheritance.
Create a parent class named Employee with the following properties:
- Employee Name
- Employee ID
- Basic Salary
Create a method called displayEmployeeDetails() to display employee information.
Create two child classes:
Manager Class:
- Add an
allowanceproperty. - Calculate manager salary by adding allowance to basic salary.
Developer Class:
- Add a
projectBonusproperty. - Calculate developer salary by adding project bonus to basic salary.
Create one object of each child class and display their details.
PHP Solution
<?php
class Employee
{
protected $name;
protected $employeeId;
protected $basicSalary;
public function __construct($name, $employeeId, $basicSalary)
{
$this->name = $name;
$this->employeeId = $employeeId;
$this->basicSalary = $basicSalary;
}
public function displayEmployeeDetails()
{
echo "Employee Name: " . $this->name . "<br>";
echo "Employee ID: " . $this->employeeId . "<br>";
echo "Basic Salary: Rs. " . $this->basicSalary . "<br>";
}
}
class Manager extends Employee
{
private $allowance;
public function __construct($name, $employeeId, $basicSalary, $allowance)
{
parent::__construct($name, $employeeId, $basicSalary);
$this->allowance = $allowance;
}
public function displayManagerDetails()
{
$this->displayEmployeeDetails();
$totalSalary = $this->basicSalary + $this->allowance;
echo "Allowance: Rs. " . $this->allowance . "<br>";
echo "Total Salary: Rs. " . $totalSalary . "<br>";
}
}
class Developer extends Employee
{
private $projectBonus;
public function __construct($name, $employeeId, $basicSalary, $projectBonus)
{
parent::__construct($name, $employeeId, $basicSalary);
$this->projectBonus = $projectBonus;
}
public function displayDeveloperDetails()
{
$this->displayEmployeeDetails();
$totalSalary = $this->basicSalary + $this->projectBonus;
echo "Project Bonus: Rs. " . $this->projectBonus . "<br>";
echo "Total Salary: Rs. " . $totalSalary . "<br>";
}
}
$manager = new Manager("Amit", 101, 50000, 10000);
$developer = new Developer("Rahul", 102, 40000, 8000);
echo "Manager Details:<br>";
$manager->displayManagerDetails();
echo "<br>Developer Details:<br>";
$developer->displayDeveloperDetails();
?>
Sample Output
Manager Details:
Employee Name: Amit
Employee ID: 101
Basic Salary: Rs. 50000
Allowance: Rs. 10000
Total Salary: Rs. 60000
Developer Details:
Employee Name: Rahul
Employee ID: 102
Basic Salary: Rs. 40000
Project Bonus: Rs. 8000
Total Salary: Rs. 48000
Explanation
Employeeis the parent class containing common employee information.- Its constructor initializes the employee name, ID and basic salary.
ManagerandDeveloperinherit fromEmployee.- Both child classes have their own additional properties.
- Their constructors use
parent::__construct()to initialize the common employee information. Managercalculates salary using its allowance.Developercalculates salary using its project bonus.- Both child classes reuse the parent’s
displayEmployeeDetails()method.
This example demonstrates how inheritance reduces code repetition and allows different employee types to have their own additional features.
Concepts Covered
- Practical inheritance
- Parent and child classes
- Multilevel class functionality
- Constructors in inheritance
parent::__construct()- Protected properties
- Method reuse
- Different child class behavior
Key Takeaways
After completing this chapter, you should understand:
- Inheritance allows one class to reuse another class’s properties and methods.
- The class being inherited from is called the parent class.
- The class that inherits is called the child class.
- PHP uses the
extendskeyword to create inheritance. - Child classes can access inherited public and protected members.
- Private members cannot be accessed directly by child classes.
- The
parentkeyword is used to call parent class methods and constructors. - Method overriding allows a child class to provide its own version of an inherited method.
- PHP supports single, multilevel and hierarchical inheritance.
- PHP does not support multiple inheritance through classes.
- Inheritance helps reduce code repetition and organize related classes.
Frequently Asked Questions (FAQs)
1. What is inheritance in PHP?
Inheritance is an Object-Oriented Programming concept that allows one class to reuse the properties and methods of another class.
2. Which keyword is used for inheritance in PHP?
PHP uses the extends keyword to create a child class from a parent class.
3. What is a parent class in PHP?
A parent class is a class whose properties and methods are inherited by another class. It is also called a base class.
4. What is a child class in PHP?
A child class is a class that inherits properties and methods from another class. It is also called a derived class.
5. What is single inheritance in PHP?
Single inheritance occurs when one child class inherits from one parent class.
6. What is multilevel inheritance in PHP?
Multilevel inheritance occurs when classes inherit from one another in a chain. For example, Manager inherits from Employee, and Employee inherits from Person.
7. What is hierarchical inheritance in PHP?
Hierarchical inheritance occurs when two or more child classes inherit from the same parent class.
8. Does PHP support multiple inheritance?
No. PHP does not support multiple inheritance through classes. However, PHP provides traits and interfaces for sharing functionality and defining common contracts.
9. What is the use of the parent keyword in PHP?
The parent keyword allows a child class to call a method or constructor defined in its parent class. For example, parent::__construct() calls the parent constructor.
10. What is method overriding in PHP?
Method overriding occurs when a child class defines a method with the same name as an inherited parent method, providing its own implementation.
11. Can a child class access private properties of a parent class?
No. Private properties are accessible only inside the class where they are declared. A parent class can provide public or protected methods to allow controlled access.
12. What is the difference between inheritance and encapsulation?
Inheritance allows classes to reuse properties and methods from other classes. Encapsulation protects class data and controls how that data is accessed or modified.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
