PHP Inheritance Tutorial

11/22/2025

markdown

Go Back

PHP Inheritance Tutorial: Extending Classes in Object-Oriented PHP

Inheritance is one of the most powerful features of Object-Oriented Programming (OOP). It allows a class (child class) to inherit properties and methods from another class (parent class). This helps you write cleaner, reusable, and organized code.

In this tutorial, you’ll learn:

  • What inheritance is

  • How inheritance works in PHP

  • Types of inheritance PHP supports

  • Method overriding

  • Using parent:: to access parent methods

  • Real-world examples


markdown

🔹 What Is Inheritance in PHP?

Inheritance allows one class to use the properties and methods of another class.

Parent Class → Base class

Child Class → Derived class that extends parent class

You use the extends keyword in PHP:

class ChildClass extends ParentClass {
}

🔸 1. Basic Example of Inheritance

class Animal {
    public function eat() {
        echo "Animal is eating";
    }
}

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

$dog = new Dog();
$dog->eat();   // inherited from Animal
$dog->bark();  // Dog’s own method

Output:

Animal is eating
Dog is barking

🔸 2. Inheriting Properties

class Vehicle {
    public $brand = "Tata";

    public function showBrand() {
        echo "Brand: " . $this->brand;
    }
}

class Car extends Vehicle {
}

$c = new Car();
$c->showBrand();

🔸 3. Method Overriding (Child Overrides Parent)

A child class can redefine a parent method.

class Employee {
    public function role() {
        echo "General Employee";
    }
}

class Manager extends Employee {
    public function role() {
        echo "Manager";   // overriding
    }
}

$mgr = new Manager();
$mgr->role();

🔸 4. Using parent:: to Access Parent Methods

You can call the parent’s method from the overridden child method.

class Student {
    public function details() {
        echo "Student details";
    }
}

class CollegeStudent extends Student {
    public function details() {
        parent::details();  // call parent method
        echo " (College Level)";
    }
}

$cs = new CollegeStudent();
$cs->details();

Output:

Student details (College Level)

🔸 5. Constructors in Inheritance

If the parent class has a constructor, the child can call it using parent::__construct().

class User {
    public function __construct() {
        echo "User created";
    }
}

class Admin extends User {
    public function __construct() {
        parent::__construct();
        echo " and Admin initialized";
    }
}

$a = new Admin();

Output:

User created and Admin initialized

🔸 6. Protected Members in Inheritance

Protected properties/methods can be accessed by child classes.

class Account {
    protected $balance = 1000;
}

class SavingsAccount extends Account {
    public function showBalance() {
        echo $this->balance;  // allowed
    }
}

$sa = new SavingsAccount();
$sa->showBalance();

🔸 7. PHP Supports Only Single Inheritance

PHP does not support multiple inheritance:

class A {}
class B {}
class C extends A, B {} // ❌ Not allowed

Instead, PHP provides traits for reusable code across classes.


🔸 8. Real-World Example: User Roles

class User {
    public $name;

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

    public function getRole() {
        return "User";
    }
}

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

class Editor extends User {
    public function getRole() {
        return "Editor";
    }
}

$admin = new Admin("Shubham");
echo $admin->name . " - " . $admin->getRole();

✅ Summary

You learned how inheritance works in PHP:

  • Child classes extend parent classes

  • Methods and properties can be reused

  • Overriding allows custom behavior

  • parent:: calls parent methods

  • PHP supports single inheritance only

Inheritance helps reduce code repetition and improves scalability.


Meta Description

Learn PHP inheritance with simple examples. Understand extends, method overriding, parent keyword, and real-world OOP examples.

Meta Keywords

php inheritance tutorial, php extends class, php parent method, php oop inheritance, php overriding

Alt Tag

"PHP inheritance tutorial with class and object examples"