PHP extends Keyword
last modified November 25, 2026
The PHP extends keyword creates a parent-child relationship between classes. The child inherits the public and protected members of the parent, can override methods, and can add behavior of its own.
This article covers single inheritance, overriding, parent::, protected state, parent constructors, multi-level hierarchies, and the errors raised by a second parent or a missing parent class.
Basic definitions
extends declares that one class is a specialization of another. The child class inherits the public and protected properties and methods of the parent, and it may add members or replace inherited methods.
PHP allows single inheritance: a class names exactly one parent. The child is a subtype of the parent, so a Car can be used wherever a Vehicle is expected. Private members stay inside the parent and are not reachable from the child. Constructors are special: if the child declares one, it must call parent::__construct() itself when the parent has initialization work to do.
The syntax is class Child extends Parent { ... }. The parent must be declared before the child is used, normally through an autoloader. Interfaces use implements rather than extends; the implements keyword article covers that side. The PHP OOP tutorial covers the wider object model.
A first subclass
The first example gives a vehicle a method that every vehicle shares, and lets a car add a method of its own.
<?php
declare(strict_types=1);
class Vehicle
{
public function start(): string
{
return "Engine started";
}
}
class Car extends Vehicle
{
public function drive(): string
{
return "Car is moving";
}
}
$car = new Car();
echo $car->start(), "\n";
echo $car->drive(), "\n";
The Car class declares only drive, yet the object also answers start. Method lookup begins at the object's class and walks up the parent chain, so an inherited method behaves as if it were declared in the child. The parent class is unchanged and can still be instantiated on its own.
$ php ext_basic.php Engine started Car is moving
Overriding a method
A child may replace an inherited method with its own version. The replacement uses the same name and a compatible signature.
<?php
declare(strict_types=1);
class Animal
{
public function speak(): string
{
return "...";
}
}
class Dog extends Animal
{
public function speak(): string
{
return "Woof";
}
}
$animals = [new Animal(), new Dog()];
foreach ($animals as $animal) {
echo $animal->speak(), "\n";
}
Both objects are stored in one array and called through the same speak method. PHP dispatches on the object's actual class, so Animal returns the parent text and Dog returns the override. This is polymorphism: the caller does not need to know which subclass it holds. PHP checks that the override keeps the parameter and return types compatible with the parent.
$ php ext_override.php ... Woof
Calling the parent method
An override often wants to extend the parent behavior rather than replace it. parent:: reaches the parent implementation from inside the child.
<?php
declare(strict_types=1);
class Report
{
protected string $title;
public function __construct(string $title)
{
$this->title = $title;
}
public function render(): string
{
return "Report: " . $this->title;
}
}
class HtmlReport extends Report
{
public function render(): string
{
return "<h1>" . parent::render() . "</h1>";
}
}
$report = new HtmlReport("Monthly sales");
echo $report->render(), "\n";
The child replaces render but reuses the parent's text through parent::render(). parent:: selects the parent implementation for that one call; it does not change $this, so the parent method still reads the object's $title property. This is the usual way to add behavior around an inherited method without copying its body.
$ php ext_parent.php <h1>Report: Monthly sales</h1>
Protected members
A protected member is visible inside the class and inside every subclass, but not to outside code. It is the usual visibility for state that a child is expected to maintain.
<?php
declare(strict_types=1);
class BankAccount
{
protected float $balance = 0.0;
public function balance(): float
{
return $this->balance;
}
}
class SavingsAccount extends BankAccount
{
public function deposit(float $amount): void
{
$this->balance += $amount;
}
}
$account = new SavingsAccount();
$account->deposit(100.50);
$account->deposit(50.25);
echo "Balance: ", $account->balance(), "\n";
The balance is protected, so SavingsAccount can update it directly while code outside the hierarchy can only read it through the public balance method. A private property would not be visible in the child. The protected keyword and private keyword articles cover the exact access rules.
$ php ext_protected.php Balance: 150.75
Parent constructors
A child that declares its own constructor replaces the parent constructor; the parent constructor does not run automatically. The child has to call it explicitly when the parent has initialization work to do.
<?php
declare(strict_types=1);
class Product
{
public function __construct(
protected string $name,
protected float $price,
) {
}
public function label(): string
{
return $this->name . " (" . $this->price . ")";
}
}
class Book extends Product
{
public function __construct(
string $name,
float $price,
private string $author,
) {
parent::__construct($name, $price);
}
public function label(): string
{
return parent::label() . " by " . $this->author;
}
}
$book = new Book("PHP in Practice", 39.0, "A. Writer");
echo $book->label(), "\n";
The child constructor receives all three values, passes the two that belong to Product to the parent constructor, and stores the author itself. The parent's typed properties are initialized before the child adds its own. If the child declared no constructor, the parent constructor would run automatically, but there would be no place to set the author. Note that echo prints the float 39.0 as 39.
$ php ext_constructor.php PHP in Practice (39) by A. Writer
Multiple levels of inheritance
A child can itself be a parent. Each level may override a method and call the level above it, which builds a chain of behavior.
<?php
declare(strict_types=1);
class Shape
{
public function describe(): string
{
return "shape";
}
}
class Polygon extends Shape
{
public function describe(): string
{
return "polygon (" . parent::describe() . ")";
}
}
class Triangle extends Polygon
{
public function describe(): string
{
return "triangle (" . parent::describe() . ")";
}
}
$triangle = new Triangle();
echo $triangle->describe(), "\n";
Each level overrides describe and calls the level above it. A Triangle is also a Polygon and a Shape, and the most specific override runs first. Deep chains like this are hard to follow; two or three levels are usually the practical limit, and composition is often clearer.
$ php ext_multi.php triangle (polygon (shape))
Only one parent
PHP does not support multiple inheritance. A class declaration accepts one parent, and a second name after the comma is a syntax error.
<?php
declare(strict_types=1);
class A {}
class B {}
class C extends A, B {}
The parser stops at the comma: a class declaration accepts one parent only. Multiple behavior is expressed with interfaces, which a class can implement in any number, or with traits, which copy methods into the class. The traits tutorial and interface keyword article cover those alternatives.
$ php ext_two_parents.php
Parse error: syntax error, unexpected token ",", expecting "{" in /app/ext_two_parents.php on line 7
An undefined parent
The parent name must resolve to a declared class when the child declaration is executed. A misspelled or missing parent fails at that point.
<?php
declare(strict_types=1);
class Child extends MissingParent {}
The script fails as soon as the declaration is executed: PHP cannot link the child to a parent that does not exist, and reports Class "MissingParent" not found. In a larger program the parent usually comes from another file, so the autoloader must be able to resolve the name. A typo in the parent name produces the same error.
$ php ext_missing.php
Fatal error: Uncaught Error: Class "MissingParent" not found in /app/ext_missing.php:5
Stack trace:
#0 {main}
thrown in /app/ext_missing.php on line 5
Best practices
Inheritance is powerful, but it couples the child to the parent's implementation. Use it when the relationship is genuinely is-a.
- Model a real is-a relationship: a
Caris aVehicle, but anEngineis not. - Keep hierarchies shallow, two or three levels at most, and prefer composition for reusable behavior.
- Call
parent::__construct()when the parent has initialization work. - Keep overrides substitutable: accept at least the parent's inputs and return a compatible value.
- Mark a class
finalwhen extension is not part of the design; the final keyword article covers that lock. - Do not use inheritance just to reach a protected helper; move shared code to a trait or a separate service.
Source
PHP manual: object inheritance
PHP manual: constructors and destructors
This article covered the PHP extends keyword: single inheritance, overriding, parent::, protected members, parent constructors, multi-level hierarchies, and the errors raised by an invalid parent. Continue with PHP final keyword for classes that cannot be extended, or read PHP abstract keyword and PHP traits for the other ways to share behavior.
Author
List all PHP tutorials.