extends
The "extends" keyword is a feature of object-oriented programming in PHP that allows a child class to inherit properties and methods from a parent class. In
The PHP extends Keyword
extends is how a class declares inheritance in PHP: a child (or subclass) built with extends automatically gains the non-private properties and methods of its parent (or superclass). Instead of copying shared code into every related class, you write it once in a parent and let subclasses specialize it.
This page covers the syntax, how to call the parent's code with parent::, how method overriding works, the access-modifier rules that decide what a child inherits, and the common mistakes people hit. PHP supports single inheritance only — a class can extends exactly one parent (use interfaces when you need a type to play several roles).
Syntax
class ChildClass extends ParentClass {
// additional or overriding members
}The child can do three things:
- Inherit members from the parent unchanged.
- Add new properties and methods of its own.
- Override an inherited method by redeclaring it with the same name.
A first example
<?php
class Fruit {
public $name;
public $color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function getInfo() {
echo "This fruit is a {$this->name} and it is {$this->color}.";
}
}
// Apple inherits everything from Fruit and overrides getInfo().
class Apple extends Fruit {
public function getInfo() {
echo "This fruit is an {$this->name} and it is {$this->color}.";
}
}
$apple = new Apple("apple", "red");
$apple->getInfo();
// Output: This fruit is an apple and it is red.Apple never declares $name, $color, or a constructor — it inherits them from Fruit. It only redefines getInfo(), which overrides the parent's version.
Calling the parent with parent::
A child often wants to extend the parent's behavior rather than replace it. When a child defines its own constructor, PHP does not call the parent constructor automatically — you must call it explicitly with parent::__construct(). The same parent:: syntax works for any overridden method.
<?php
class Car {
public $model;
public $year;
public function __construct($model, $year) {
$this->model = $model;
$this->year = $year;
}
public function getInfo() {
echo "This car is a {$this->model} from {$this->year}.";
}
}
class Toyota extends Car {
public function __construct($year) {
// Fill in the model, defer the rest to the parent.
parent::__construct("Toyota", $year);
}
public function getInfo() {
parent::getInfo(); // reuse the parent's output...
echo " (built by Toyota)"; // ...then add to it.
}
}
$toyota = new Toyota(2021);
$toyota->getInfo();
// Output: This car is a Toyota from 2021. (built by Toyota)If Toyota had no constructor at all, it would inherit Car's constructor and you would create it with new Toyota("Corolla", 2021).
What a child can — and cannot — inherit
Access modifiers decide visibility across the inheritance boundary:
publicmembers are inherited and usable everywhere.protectedmembers are inherited and accessible inside the child, but not from outside the class.privatemembers are not accessible in the child. They still exist on the object, but the child cannot see them by name.
<?php
class Account {
public $owner = "Ann";
protected $balance = 100; // visible to children
private $pin = "1234"; // hidden from children
public function reveal() {
return "$this->owner, balance $this->balance, pin $this->pin";
}
}
class SavingsAccount extends Account {
public function summary() {
// $this->balance works (protected); $this->pin would be null/undefined.
return "{$this->owner} has {$this->balance}";
}
}
$s = new SavingsAccount();
echo $s->summary();
// Output: Ann has 100See PHP access modifiers for the full public / protected / private rules.
Sealing a class or method with final
Use the final keyword to stop further extension. final class cannot be a parent; a final method cannot be overridden by a subclass. This is useful when overriding would break an invariant your class relies on.
class PaymentGateway {
final public function charge() { /* must not be altered */ }
}Common gotchas
- Forgetting
parent::__construct(). Defining a child constructor shadows the parent's. If you forget to call it, parent properties stay uninitialized. - Single inheritance.
class A extends B, Cis a syntax error. Compose behavior with interfaces or traits instead. - Private members aren't shared. A
privateproperty on the parent is invisible to the child; make itprotectedif subclasses need it. - Override signatures must stay compatible. An overriding method must accept the same (or wider) arguments, or PHP raises a warning.
Related topics
- PHP Inheritance — the broader concept
extendsimplements. - PHP Classes and Objects — the building blocks.
- Abstract classes — parents that must be extended.
- Interfaces — contracts for unrelated classes.