PHP Inheritance in OOP: Extending Classes in PHP

Inheritance is one of the core concepts of object-oriented programming in PHP. It allows a class to reuse the properties and methods of another class without rewriting the same code again.

A common situation in real projects is having multiple classes that share some common behaviour. For example, an application may have different types of users such as administrators, customers, and editors. All of them may need properties like name and email, but each type of user can have its own additional features.

Copy-pasting the same methods into multiple classes works fine until a small change arrives. Then you discover that fixing one class is easy, but finding the other four copies is the real adventure.

Instead of copying the same code into every class, PHP inheritance lets you create a common parent class and extend it where needed. The extends keyword is used to create this relationship.

What is inheritance in PHP?

PHP inheritance allows one class (child class) to inherit the properties and methods of another class (parent class). The child class can use the inherited members directly and can also add new functionality or modify existing behaviour.

The class that provides the existing functionality is called the parent class, base class, or superclass. The class that inherits from it is called the child class, derived class, or subclass.

The basic inheritance syntax looks like this:

<?php

class ParentClass
{
    // Properties and methods
}

class ChildClass extends ParentClass
{
    // Additional properties and methods
}

?>

The child class automatically gets access to the accessible properties and methods defined in the parent class.

PHP inheritance example

Consider a simple example where a website has different types of employees. Every employee has a name and email address, but managers have additional responsibilities.

Instead of creating separate classes with duplicate code, we can create a common Employee class and extend it.

<?php

class Employee
{
    public string $name;
    public string $email;

    public function displayDetails()
    {
        echo "Name: " . $this->name . "<br>";
        echo "Email: " . $this->email . "<br>";
    }
}

class Manager extends Employee
{
    public string $department;

    public function displayDepartment()
    {
        echo "Department: " . $this->department;
    }
}

$manager = new Manager();

$manager->name = "John";
$manager->email = "john@example.com";
$manager->department = "Engineering";

$manager->displayDetails();
$manager->displayDepartment();

?>

Here, the Manager class does not define the name, email, or displayDetails() method. It inherits them from the Employee class.

The child class only contains behaviour that is specific to managers.

Using the extends keyword in PHP

The extends keyword creates an inheritance relationship between two classes.

The following example shows the relationship clearly:

<?php

class Vehicle
{
    public function start()
    {
        echo "Vehicle started";
    }
}

class Car extends Vehicle
{
}

$car = new Car();
$car->start();

?>

The Car class does not have a start() method, but it can still call it because it inherits the method from Vehicle.

This is the main benefit of inheritance: common functionality can be written once and reused by multiple classes.

Parent and child class relationship in PHP

A child class inherits from only one parent class in PHP. This is called single inheritance.

For example, a Manager class can extend an Employee class, but it cannot directly extend both Employee and User classes.

<?php

class User
{
    public string $username;
}

class Employee extends User
{
    public string $employeeId;
}

class Manager extends Employee
{
    public string $department;
}

?>

In this example, Manager inherits from Employee, and Employee inherits from User. This creates a multi-level inheritance chain.

The inheritance flow is:

User
 |
Employee
 |
Manager

A Manager object can access properties and methods from both parent classes, provided they have suitable visibility.

Accessing parent class methods using parent::

Sometimes a child class needs to add its own behaviour while still using the original method from the parent class.

PHP provides the parent:: keyword for this purpose. It allows a child class to call a parent class method directly.

<?php

class Employee
{
    public function getRole()
    {
        echo "Employee";
    }
}

class Manager extends Employee
{
    public function getRole()
    {
        parent::getRole();
        echo " - Manager";
    }
}

$manager = new Manager();
$manager->getRole();

?>

Output:

Employee - Manager

The child class overrides the getRole() method but still reuses the parent implementation with parent::getRole().

This pattern is useful when the child class needs to extend existing behaviour instead of completely replacing it.

Method overriding in PHP inheritance

Method overriding happens when a child class defines a method with the same name as a method in its parent class.

The child implementation replaces the parent implementation when the method is called on the child object.

<?php

class Notification
{
    public function send()
    {
        echo "Sending notification";
    }
}

class EmailNotification extends Notification
{
    public function send()
    {
        echo "Sending email notification";
    }
}

$notification = new EmailNotification();
$notification->send();

?>

Output:

Sending email notification

The EmailNotification class overrides the send() method because email notifications require different behaviour.

When overriding methods, the child method must follow PHP’s visibility rules. A child class cannot reduce the visibility of an inherited method.

For example, a public method in the parent class cannot become protected or private in the child class.

Inheritance and visibility in PHP

The visibility of properties and methods determines whether child classes can access inherited members.

Visibility Accessible in child class? Description
public Yes Accessible everywhere.
protected Yes Accessible inside the class and child classes.
private No Accessible only inside the class where it is declared.

In real applications, protected is often useful for inherited properties because it allows child classes to reuse the value while preventing direct access from outside the class.

<?php

class User
{
    protected string $name = "John";

    public function getName()
    {
        return $this->name;
    }
}

class Customer extends User
{
    public function showName()
    {
        echo $this->name;
    }
}

$customer = new Customer();
$customer->showName();

?>

The Customer class can access the protected $name property because it inherits from User.

Calling parent constructors in PHP inheritance

When a child class has its own constructor, PHP does not automatically call the parent class constructor. If the parent constructor contains important initialization logic, the child class must call it explicitly using parent::__construct().

<?php

class User
{
    protected string $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }
}

class Customer extends User
{
    private string $customerType;

    public function __construct(string $name, string $customerType)
    {
        parent::__construct($name);
        $this->customerType = $customerType;
    }

    public function display()
    {
        echo "Name: " . $this->name . "<br>";
        echo "Type: " . $this->customerType;
    }
}

$customer = new Customer("David", "Premium");
$customer->display();

?>

Here, the Customer constructor calls the parent constructor to initialize the inherited $name property.

A common mistake is creating a child constructor and forgetting the parent constructor call. The code may run without errors, but inherited properties may remain uninitialized.

Final classes and preventing inheritance

PHP allows you to prevent a class from being extended by using the final keyword.

<?php

final class PaymentProcessor
{
    public function process()
    {
        echo "Processing payment";
    }
}

class OnlinePayment extends PaymentProcessor
{
}

?>

The above code produces a fatal error because PaymentProcessor cannot be inherited.

Fatal error: Class OnlinePayment may not inherit from final class PaymentProcessor

A class is usually marked as final when extending it could create unexpected behaviour or when the implementation should remain unchanged.

PHP also allows methods to be marked as final. This prevents child classes from overriding specific methods.

<?php

class Report
{
    final public function generate()
    {
        echo "Generating report";
    }
}

class SalesReport extends Report
{
    public function generate()
    {
        echo "Generating sales report";
    }
}

?>

The generate() method cannot be overridden because it is declared as final.

Inheritance vs composition in PHP

Inheritance is useful, but it is not always the best design choice. A common mistake in object-oriented programming is using inheritance just because two classes look related.

The rule of thumb is:

  • Use inheritance when the child class is a type of the parent class.
  • Use composition when one class uses another class.

For example, a Manager is an Employee, so inheritance makes sense.

class Manager extends Employee
{
}

But a Car is not an Engine. A car has an engine. This relationship is better represented using composition.

<?php

class Engine
{
    public function start()
    {
        echo "Engine started";
    }
}

class Car
{
    private Engine $engine;

    public function __construct()
    {
        $this->engine = new Engine();
    }

    public function start()
    {
        $this->engine->start();
    }
}

?>

Modern PHP applications often prefer composition because it keeps classes more flexible and easier to maintain.

Inheritance is still valuable when there is a clear parent-child relationship and the child genuinely represents a specialized version of the parent.

Common PHP inheritance mistakes

1. Creating deep inheritance chains

A long inheritance hierarchy can make code difficult to understand. If a class depends on behaviour inherited through several levels, finding where that behaviour comes from becomes harder.

Prefer simple inheritance structures and use interfaces or composition when the design becomes complicated.

2. Overriding methods without preserving behaviour

When overriding a parent method, completely replacing the original behaviour may break assumptions made by other parts of the application.

If the parent logic is still required, call it using parent:: and add the child-specific behaviour.

3. Using private properties expecting child access

A private property belongs only to the class where it is declared. Child classes cannot access it directly.

If a child class needs access, use a protected property or provide a public/protected method to expose the required value.

PHP inheritance and interfaces

Inheritance allows a class to reuse code from one parent class. However, PHP does not support multiple class inheritance. A class cannot extend more than one class at the same time.

When a class needs to follow multiple contracts, PHP interfaces are usually the better choice.

For example, a payment system may have different payment classes that all need a pay() method. They do not need to inherit from the same class, but they must follow the same structure.

<?php

interface Payment
{
    public function pay(float $amount);
}

class CreditCardPayment implements Payment
{
    public function pay(float $amount)
    {
        echo "Paid " . $amount . " using credit card";
    }
}

class PaypalPayment implements Payment
{
    public function pay(float $amount)
    {
        echo "Paid " . $amount . " using PayPal";
    }
}

?>

A class can implement multiple interfaces, which provides a flexible alternative when inheritance does not fit the design.

You can learn more about interfaces in the official PHP documentation:
PHP interfaces.

PHP inheritance best practices

Inheritance is powerful, but using it carefully results in cleaner and more maintainable code.

  • Create a parent class only when there is a genuine common relationship.
  • Keep inheritance hierarchies shallow.
  • Use protected carefully. Too many protected members can tightly couple parent and child classes.
  • Use interfaces when different classes need to provide the same behaviour.
  • Prefer composition when a class only needs another class’s functionality.
  • Use the final keyword when a class or method should not be extended or modified.

A good inheritance design should make the code easier to understand. If adding inheritance makes the relationship confusing, it is usually a sign that another design approach may work better.

Frequently asked questions about PHP inheritance

Can a PHP class inherit from multiple classes?

No. PHP supports single class inheritance. A class can extend only one parent class.

However, a class can implement multiple interfaces and can also use multiple traits to reuse functionality.

What is the difference between extends and implements in PHP?

The extends keyword is used when one class inherits properties and methods from another class.

The implements keyword is used when a class agrees to follow the methods defined by an interface.

class Admin extends User
{
}

class EmailService implements NotificationInterface
{
}

Can private methods be inherited in PHP?

No. Private methods are only available inside the class where they are declared. A child class cannot access or override private methods.

If a child class needs to customize behaviour, use protected or public visibility instead.

Can constructors be inherited in PHP?

A child class inherits the parent constructor only when it does not define its own constructor. If the child class has its own constructor, it must call the parent constructor manually using parent::__construct() when required.

Conclusion

PHP inheritance helps reduce duplicate code by allowing child classes to reuse and extend existing functionality from parent classes.

The extends keyword creates the inheritance relationship, while parent:: helps reuse parent implementations when overriding methods. Understanding visibility rules, constructors, and when to use composition instead of inheritance will help you design cleaner object-oriented PHP applications.

Photo of Vincy, PHP developer
Written by Vincy Last updated: July 22, 2026
I'm a PHP developer with 20+ years of experience and a Master's degree in Computer Science. I build and improve production PHP systems for eCommerce, payments, webhooks, and integrations, including legacy upgrades (PHP 5/7 to PHP 8.x).

Continue Learning

These related tutorials may help you continue learning.

Leave a Reply

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

Explore topics
Need PHP help?