Inheritance in PHP

Introduction

Inheritance is one of the most important concepts in Object-Oriented Programming (OOP). It allows one class to inherit the properties and methods of another class.

In simple words, inheritance helps you reuse existing code instead of rewriting it. This makes your code cleaner, more structured, and easier to maintain.

In this tutorial, you will learn what inheritance is, how it works in PHP, and why it is useful in real-world applications.

What is Inheritance?

Inheritance allows a class to reuse properties and methods of another class.

  • Parent class → base class
  • Child class → derived class
  • Child class uses extends keyword

This helps avoid code duplication.

Why Use Inheritance?

Inheritance helps you:

  • Reuse existing code
  • Avoid duplication
  • Maintain clean structure
  • Create logical class hierarchy
  • Maintain large applications easily
  • Follow DRY principle (Don’t Repeat Yourself)

Basic Inheritance Example

<?php
class User {
    public $name;

    public function setName($name) {
        $this->name = $name;
    }
}
?>

Creating a Child Class

<?php
class Admin extends User {
    public function showRole() {
        echo "Admin User";
    }
}
?>

Using Parent & Child Class

<?php
$admin = new Admin();
$admin->setName("Divyesh");

echo $admin->name;
$admin->showRole();
?>

Admin class uses methods from User

Method Overriding

A child class can override a parent method.

<?php
class User {
    public function getRole() {
        return "User";
    }
}

class Admin extends User {
    public function getRole() {
        return "Admin";
    }
}
?>

Overridden Method Output

<?php
$admin = new Admin();
echo $admin->getRole();
?>

Output:

Admin

Using parent:: Keyword

To access parent method inside child class:

<?php
class Admin extends User {
    public function getRole() {
        return parent::getRole() . " (Admin Access)";
    }
}
?>

Inheritance with Access Modifiers

  • public → accessible everywhere
  • protected → accessible in child class
  • private → NOT accessible in child class
class User {
    protected $email;
}

✔ Child class can access $email

Real-World Example

Let’s understand this with a simple example.

Suppose you have a class called Person.

<?php
class Person {
public $name; public function greet() {
echo "Hello, my name is " . $this->name;
}
}
?>

Now, you create a class called Student that extends Person.

<?php
class Student extends Person {
public $rollNumber; public function showRollNumber() {
echo "My roll number is " . $this->rollNumber;
}
}$student1 = new Student();
$student1->name = "Rahul";
$student1->rollNumber = 101;$student1->greet();
$student1->showRollNumber();
?>

What Happened Here?

  • Student inherited the greet() method from Person
  • We did not rewrite the greet method
  • Code reuse happened automatically

This is the power of inheritance.

Using Protected with Inheritance

The protected access modifier works especially well with inheritance.

  • private → cannot be accessed in child class
  • protected → can be accessed in child class

Example:

<?php
class ParentClass {
protected $name = "Rahul";
}class ChildClass extends ParentClass {
public function showName() {
echo $this->name;
}
}$obj = new ChildClass();
$obj->showName();
?>

Here, the child class can access the protected property.

Important Points to Remember

  • A class can extend only one parent class in PHP
  • Inheritance creates an “is-a” relationship
  • Child classes can add new methods
  • Child classes can override parent methods
  • Use protected when child classes need access

Limitations of Inheritance

  • PHP supports single inheritance only
  • A class can extend only one parent class

(Interfaces solve this — coming later)

Best Practices

  • Keep parent class generic
  • Avoid deep inheritance chains
  • Prefer composition when possible (advanced topic)

Practical Task

  • Create a class called Vehicle. Then add a method in it called start().
  • Create another class called Car that extends Vehicle. Then add a method drive() in it.
  • Create an object of Car can call both the methods.
  • Observe how the child class can use methods from the parent class.

Summary

  • Inheritance allows one class to inherit features from another class
  • It is implemented using the extends keyword
  • It promotes code reuse and cleaner structure
  • Child classes can override parent methods
  • Protected members can be accessed in child classes

Inheritance is a powerful concept in OOP and is widely used in modern PHP applications.

In the next tutorial, we’ll learn about Encapsulation in PHP.

Related Tutorials

  • Introduction to Object-Oriented Programming (OOP) in PHP

    Introduction As PHP projects grow, managing code using simple procedural programming becomes difficult. Files become longer, functions are scattered, and maintaining the project becomes time-consuming. This is where Object-Oriented Programming (OOP) becomes useful. OOP helps you organize your PHP code in a structured way by grouping related data and functionality together. Instead of writing everything…

  • Classes & Objects in PHP

    Introduction In Object-Oriented Programming (OOP), classes and objects are the foundation of everything. Understanding these two concepts clearly will make the rest of OOP much easier to learn. In simple terms, a class is a blueprint, and an object is a real instance created from that blueprint. In this tutorial, you will learn what classes…

  • Constructors & Destructors in PHP

    Introduction In Object-Oriented Programming, constructors and destructors are special methods that automatically run at specific moments in an object’s lifecycle. A constructor runs when an object is created.A destructor runs when an object is destroyed. These methods help initialize and clean up resources in a PHP application. Understanding them is essential for writing structured and…

  • Access Modifiers in PHP (public, private, protected)

    Introduction In Object-Oriented Programming, access modifiers control the visibility of properties and methods inside a class. They determine: In PHP, there are three main access modifiers: Understanding access modifiers is important because they help protect data and improve code security and structure. Why Are Access Modifiers Important? Without access control, anyone could change the internal…

  • Encapsulation in PHP

    Introduction Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It refers to the concept of restricting direct access to certain parts of an object and allowing controlled access through methods. In simple words, encapsulation means data hiding. Instead of allowing users to directly access and modify object properties, we protect them using…

  • Polymorphism in PHP

    Introduction Polymorphism is one of the four core principles of Object-Oriented Programming (OOP): The word Polymorphism means “many forms.” In PHP, polymorphism allows different classes to use the same method name but perform different actions. This makes your code flexible, scalable, and easier to maintain. 📌 What is Polymorphism? Polymorphism allows objects of different classes…

Leave a Reply

Your email address will not be published. Required fields are marked *