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.

Sentry image

Speed up your PHP app without the yak-shaving

From Docker flamecharts to tracing requests across services, this guide shows you how to debug PHP performance issues without turning into a full-time APM engineer. Bonus: tips that don’t require rewriting your app.

Read more →

Top comments (0)

Coherence image

Authenticated AI Chat, Just 15 Lines of Code

Multi-modal streaming chat (including charts) with your existing backend data. Choose from 10+ models from all leading providers. Total control and visibility.

Learn more

👋 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