DEV Community

Shariful Ehasan
Shariful Ehasan

Posted on • Edited on

2 1 1 1

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 "<br>";
echo $dog->bark(); // Output: Buddy says Woof!

Enter fullscreen mode Exit fullscreen mode

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.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhere—across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

Top comments (0)

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay