๐Ÿ” 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 :

Leave a Reply

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

Share

๐Ÿ” PHP Inheritance

Or Copy Link

CONTENTS
Scroll to Top