Inheritance in PHP OOP: A Simple Guide

Inheritance is a key feature of Object-Oriented Programming (OOP) in PHP. It allows one class (called a child class) to inherit properties and methods from another class (called a parent class). This helps reuse code and make your program more organized. What is Inheritance? Think of inheritance like a parent passing traits to their child. In PHP, a child class can use everything from the parent class and add its own features if needed. How to Use Inheritance in PHP To create inheritance, you use the extends keyword. The child class extends the parent class to inherit its properties and methods. Example Code Below is a simple example of inheritance in PHP: // Parent class class Animal { public $name; public function eat() { return "$this->name is eating."; } } // Child class inheriting from Animal class Dog extends Animal { public function bark() { return "$this->name says Woof!"; } } // Create an object of the child class $dog = new Dog(); $dog->name = "Buddy"; // Use methods from both parent and child class echo $dog->eat(); // Output: Buddy is eating. echo ""; echo $dog->bark(); // Output: Buddy says Woof! Explanation Parent Class (Animal): This class has a property $name and a method eat(). Child Class (Dog): This class uses extends Animal to inherit from Animal. It also adds its own method bark(). Object Creation: We create a Dog object and set its $name to "Buddy". Method Calls: The Dog object can use both eat() (from the parent) and bark() (from itself). Benefits of Inheritance Code Reusability: You don’t need to rewrite the same code for similar classes. Easy Maintenance: Changes in the parent class automatically apply to child classes. Organized Code: It keeps related classes connected in a clear way. Important Notes A child class can only extend one parent class in PHP (single inheritance). Use the protected keyword in the parent class if you want properties or methods to be accessible only to the parent and child classes. You can override parent methods in the child class by redefining them.

Jun 24, 2025 - 08:20
 0
Inheritance in PHP OOP: A Simple Guide

Inheritance is a key feature of Object-Oriented Programming (OOP) in PHP. It allows one class (called a child class) to inherit properties and methods from another class (called a parent class). This helps reuse code and make your program more organized.

What is Inheritance?

Think of inheritance like a parent passing traits to their child. In PHP, a child class can use everything from the parent class and add its own features if needed.

How to Use Inheritance in PHP

To create inheritance, you use the extends keyword. The child class extends the parent class to inherit its properties and methods.

Example Code

Below is a simple example of inheritance in PHP:


// Parent class
class Animal {
    public $name;

    public function eat() {
        return "$this->name is eating.";
    }
}

// Child class inheriting from Animal
class Dog extends Animal {
    public function bark() {
        return "$this->name says Woof!";
    }
}

// Create an object of the child class
$dog = new Dog();
$dog->name = "Buddy";

// Use methods from both parent and child class
echo $dog->eat();  // Output: Buddy is eating.
echo "
"; echo $dog->bark(); // Output: Buddy says Woof!

Explanation

  1. Parent Class (Animal): This class has a property $name and a method eat().

  2. Child Class (Dog): This class uses extends Animal to inherit from Animal. It also adds its own method bark().

  3. Object Creation: We create a Dog object and set its $name to "Buddy".

  4. Method Calls: The Dog object can use both eat() (from the parent) and bark() (from itself).

Benefits of Inheritance

  • Code Reusability: You don’t need to rewrite the same code for similar classes.

  • Easy Maintenance: Changes in the parent class automatically apply to child classes.

  • Organized Code: It keeps related classes connected in a clear way.

Important Notes

  • A child class can only extend one parent class in PHP (single inheritance).

  • Use the protected keyword in the parent class if you want properties or methods to be accessible only to the parent and child classes.

  • You can override parent methods in the child class by redefining them.