PHP Inheritance: Complete Guide with Examples

1. Introduction

Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). In PHP, inheritance allows one class to use the properties and methods of another class.

The class that is inherited from is called the Parent Class (Base Class), and the class that inherits the properties and methods is called the Child Class (Derived Class).

In PHP, inheritance is implemented using the extends keyword.

What is IS-A relationship?

Inheritance

In this image, Indra Gandhi is a Politician and a Daughter. So this is called an Is a relationship.

📖
Benefits of PHP Inheritance:
  • Code reusability
  • Better code organization
  • Easier maintenance
  • Faster development

2. Syntax

The basic syntax of PHP inheritance is very simple. A child class uses the extends keyword to inherit from a parent class.


class ParentClass {
   // properties and methods
}
class ChildClass extends ParentClass {
   // additional properties and methods
}

Here:

  • ParentClass contains common functionality.
  • ChildClass inherits all public and protected members of ParentClass.
  • The child class can also add its own properties and methods.

Private members of the parent class cannot be accessed directly by the child class.

3. Example

Below is a simple example to understand PHP inheritance.



class Animal {
   public function sound() {
       echo "Animals make sounds";
   }
}

class Dog extends Animal {
   public function bark() {
       echo "Dog barks";
   }
}

$dog = new Dog();

$dog->sound();
echo "<br>";
$dog->bark();

In this example, the Dog class inherits from the Animal class.

Output:

Animals make sounds
Dog barks

4. Real-world Example

Let’s take a practical example related to employees in a company.

A company may have many types of employees such as Manager, Developer, and Designer. All employees share common details like name and salary.

Instead of creating these properties in every class, we create a base Employee class and then inherit it.


class Employee {

   public $name;
   public $salary;

   public function getEmployeeInfo() {
       echo "Employee Name: " . $this->name . "
"; echo "Salary: " . $this->salary . "
"; } } class Developer extends Employee { public $language; public function getDeveloperInfo() { echo "Programming Language: " . $this->language; } } $dev = new Developer(); $dev->name = "John"; $dev->salary = 50000; $dev->language = "PHP"; $dev->getEmployeeInfo(); $dev->getDeveloperInfo();

This example shows how a child class can reuse the properties and methods of a parent class.

Output:

Employee Name:John
Salary: 50000
Programming Language: PHP

5. Type of Inheritance

There are mainly 3 types of Inheritance in Php.

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical Inheritance
Inheritance Types

two other inheritance used through Interface.

  1. Multiple Inheritance
  2. Hybrid Inheritance
Hybrid Inheritance

Why Multiple inheritance is not support PHP language?

Without Interface, Multiple inheritance is not support Php language.

Without Interface Multiple inheritance program.

Multiple Inheritance

6. Conclusion

PHP inheritance is a powerful feature of Object-Oriented Programming that allows one class to reuse the properties and methods of another class.

By using inheritance, developers can build a hierarchical relationship between classes and create scalable applications. The extends keyword makes it easy to inherit functionality from a parent class while allowing the child class to add its own behavior.

PHP Inheritance – Interview Questions

Q 1: What is inheritance in PHP?
Ans: It allows one class to inherit properties of another.
Q 2: Keyword used for inheritance?
Ans: extends.
Q 3: Can PHP support multiple inheritance?
Ans: No.
Q 4: Can child class override methods?
Ans: Yes.
Q 5: Why use inheritance?
Ans: Code reusability and maintainability.

PHP Inheritance – Objective Questions (MCQs)

Q1. Inheritance allows a class to ______.






Q2. The class that inherits is called ______.






Q3. Which keyword is used for inheritance in PHP?






Q4. Private members of parent class are ______ accessible in child class.






Q5. Inheritance helps in ______.






Related PHP Tutorials