PHP Inheritance – Reuse and Extend Functionality with OOP

Learn how to use inheritance in PHP to create parent-child relationships between classes, reuse logic, and extend behavior.


Introduction – What Is Inheritance in PHP?

Inheritance is a core concept of Object-Oriented Programming (OOP) that allows one class (called the child or subclass) to inherit properties and methods from another class (called the parent or superclass).

With inheritance, you can:

  • Reuse common logic across multiple classes
  • Customize or extend behaviors in subclasses
  • Apply the DRY (Don’t Repeat Yourself) principle
  • Build hierarchical class structures (e.g., Animal → Dog → Labrador)

1. Basic Inheritance Syntax

class Animal {
    public function speak() {
        echo "Animal sound";
    }
}

class Dog extends Animal {
}

$dog = new Dog();
$dog->speak(); // Animal sound

Dog inherits the speak() method from Animal.
Use the extends keyword to inherit from a base class.


2. Overriding Parent Methods

class Dog extends Animal {
    public function speak() {
        echo "Bark!";
    }
}

$dog = new Dog();
$dog->speak(); // Bark!

You can override a method in the child class to change its behavior.
Method names remain the same but are redefined.


3. Accessing Parent Methods with parent::

class Cat extends Animal {
    public function speak() {
        parent::speak();
        echo " and Meow!";
    }
}

$cat = new Cat();
$cat->speak(); // Animal sound and Meow!

Use parent::method() to call the original implementation in the parent class.
Useful when you want to extend (not replace) the parent behavior.


4. Property Inheritance

class Vehicle {
    public $type = "Generic Vehicle";
}

class Car extends Vehicle {
    public function describe() {
        echo "This is a $this->type";
    }
}

$car = new Car();
$car->describe(); // This is a Generic Vehicle

Properties defined in the parent are accessible in the child class if declared public or protected.


5. Final Classes and Methods

final class Base {}

class Child extends Base {} //  Error: Cannot extend final class
class ParentClass {
    final public function greet() {
        echo "Hello!";
    }
}

class ChildClass extends ParentClass {
    // public function greet() {}  Error: Cannot override final method
}

Use final to prevent inheritance or overriding.
Ensures certain functionality remains untouched.


👩‍👦 6. Multilevel Inheritance

class LivingBeing {
    public function breathe() {
        echo "Breathing...";
    }
}

class Human extends LivingBeing {}

class Developer extends Human {}

$dev = new Developer();
$dev->breathe(); // Breathing...

A class can inherit from another class that already inherits from another class.
PHP does not support multiple inheritance (one parent only).


Best Practices

  • Use inheritance when classes share common behavior
  • Use protected visibility for reusable internal methods
  • Don’t overuse inheritance — consider composition when appropriate
  • Always call parent::__construct() if the parent has one
  • Avoid deep inheritance trees (more than 2–3 levels)

Summary – Recap & Next Steps

Inheritance lets you build hierarchical class relationships, reuse functionality, and avoid repetitive code. It’s one of the most powerful tools in PHP OOP — when used carefully.

Key Takeaways:

  • Use extends to inherit from a parent class
  • Override methods for customized behavior
  • Use parent:: to call the parent method
  • final blocks further inheritance or overriding

Real-World Use Cases:
User → Admin → SuperAdmin, Animal → Dog → Bulldog, Controller → ApiController, Vehicle → Car → ElectricCar


Frequently Asked Questions (FAQs)

Can a PHP class extend multiple classes?
No. PHP supports only single inheritance.

Can a child class override a parent constructor?
Yes, but you can also call parent::__construct() inside it.

What’s the difference between public, private, and protected in inheritance?
public and protected properties/methods are inherited. private is not.

When should I use final?
Use it to lock down classes or methods you don’t want altered.

Is inheritance better than composition?
Use inheritance for “is-a” relationships; use composition for “has-a” relationships.


Share Now :
Share

🔁 PHP Inheritance

Or Copy Link

CONTENTS
Scroll to Top