PHP OOP Inheritance
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows developers to create new objects from existing ones, inheriting all its
Inheritance lets one class reuse the properties and methods of another, so you can model "is-a" relationships (a Car is a Vehicle) without copying code. In PHP it is implemented with the extends keyword. This chapter covers the syntax, how method overriding and the parent keyword work, how constructors are inherited, the role of visibility and the final keyword, and a complete runnable example you can experiment with.
If you are new to classes, start with PHP Classes and Objects, then come back here.
What is Inheritance?
Inheritance is the ability to define a new class (the child, subclass, or derived class) based on an existing one (the parent, superclass, or base class). The child automatically gains the parent's public and protected members and can:
- reuse them as-is,
- override them with its own implementation, or
- add brand-new properties and methods.
PHP supports single inheritance only: a class can extend exactly one parent. You can, however, build a chain — Hatchback extends Car extends Vehicle — and a class can also implement many interfaces when you need behaviour from more than one source.
Benefits of Inheritance
- Reusability — shared logic lives in the parent, so you write it once.
- Maintainability — a fix in the parent propagates to every subclass; less duplication means fewer places for bugs to hide.
- Polymorphism — code that expects a
Vehicleworks with any subclass, so you can swap implementations freely. - Clear modelling — the class hierarchy documents the "is-a" relationships in your domain.
PHP OOP Inheritance Syntax
To create a subclass, write extends followed by the parent class name:
class ChildClass extends ParentClass {
// child-specific properties and methods
}ChildClass now has every public and protected member of ParentClass. Note that private members of the parent are not accessible from the child — use protected when you want subclasses to reach a member but keep it hidden from outside code. See PHP Access Modifiers for the full visibility rules.
Overriding Parent Methods
A subclass overrides a parent method by declaring a method with the same name. The child's version replaces the parent's for objects of that subclass:
class ParentClass {
public function displayMessage() {
echo "Hello from ParentClass";
}
}
class ChildClass extends ParentClass {
public function displayMessage() {
echo "Hello from ChildClass";
}
}Calling the parent method with parent::
Overriding does not have to mean replacing — often you want to extend the parent's behaviour. Use parent:: to call the parent's version first, then add to it:
class ChildClass extends ParentClass {
public function displayMessage() {
parent::displayMessage(); // runs the parent's logic
echo " — and hello from ChildClass";
}
}Adding the #[\Override] attribute (PHP 8.3+) above a method tells PHP to fail with an error if it does not actually override a parent method — a cheap way to catch typos in method names.
Inheriting and Extending Constructors
If a child class does not define its own constructor, the parent constructor is inherited and runs automatically. If the child does define one, the parent constructor is not called for you — you must call it explicitly with parent::__construct(...):
class Vehicle {
public function __construct(public string $make) {}
}
class Car extends Vehicle {
public function __construct(string $make, public string $fuelType) {
parent::__construct($make); // initialise the inherited part
}
}For more on constructors, see PHP Constructor.
Preventing Overriding with final
Mark a method final to stop subclasses from overriding it, or mark a whole class final to stop it from being extended at all:
class PaymentBase {
final public function transactionId(): string {
return 'TX-' . uniqid();
}
}This is useful for logic that must behave identically everywhere. See PHP final Keyword for details. When the parent should define a method's signature but force every child to supply the body, use an abstract class instead.
PHP OOP Inheritance Example
Here is a complete, runnable program. A Vehicle base class holds the common data, and a Car subclass adds a fuelType property and extends displayDetails() via parent:::
<?php
class Vehicle
{
public function __construct(
protected string $make,
protected string $model,
protected int $year
) {}
public function displayDetails(): void
{
echo "Make: {$this->make}\n";
echo "Model: {$this->model}\n";
echo "Year: {$this->year}\n";
}
}
class Car extends Vehicle
{
public function __construct(
string $make,
string $model,
int $year,
protected string $fuelType
) {
parent::__construct($make, $model, $year);
}
public function displayDetails(): void
{
parent::displayDetails(); // reuse the parent's output
echo "Fuel Type: {$this->fuelType}\n"; // then add our own
}
}
$car = new Car('Toyota', 'Corolla', 2023, 'Hybrid');
$car->displayDetails();Output:
Make: Toyota
Model: Corolla
Year: 2023
Fuel Type: HybridCar inherits make, model, and year (declared protected, so the subclass can use them), reuses the parent's displayDetails() through parent::, and adds the fuelType line of its own.
Common Gotchas
privateis not inherited. Aprivateparent property is invisible to the child. Useprotectedif subclasses need it.- Constructors aren't chained automatically when the child defines its own — always call
parent::__construct()yourself. - PHP has no multiple inheritance. A class extends one parent; use interfaces or traits to share behaviour from multiple sources.
- Overriding ignores typos silently before PHP 8.3 — a misspelled method name just creates a new method instead of overriding. Add
#[\Override]to be safe.
Conclusion
Inheritance lets a subclass reuse, override, and extend the members of a parent class through the extends keyword, with parent:: giving access to the original implementation. Combined with visibility modifiers, constructors, and final, it is the backbone of code reuse and polymorphism in PHP OOP. Next, explore abstract classes and interfaces to design cleaner class hierarchies.