PHP Inheritance
In this page:
class ParentClass {
// properties and methods
}
class ChildClass extends ParentClass {
// inherits from ParentClass, adds its own
}
What is Inheritance?
Inheritance lets one class (the child) reuse the properties and methods of another class (the parent) using the extends keyword, avoiding duplicating shared logic across related classes.
Example: What is Inheritance?
<?php
// Define the class `Animal`
class Animal {
// Define the function `eat` with no parameters
function eat() {
// Print "Eating...\n" to the output
echo "Eating...\n";
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->eat();
?>
Login to try C/C++/Java/PHP code in the editor
Defining Parent and Child
A child class automatically has access to all of its parent's public and protected members without redeclaring them, and can add new properties or methods of its own on top of what it inherited.
Example: Defining Parent and Child
<?php
// Define the class `Animal`
class Animal {
// Declare the public property `$name`, set to "Generic Animal"
public $name = "Generic Animal";
// Define the function `eat` with no parameters
function eat() {
// Print "Eating\n" to the output
echo "Eating\n";
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the function `bark` with no parameters
function bark() {
// Print "Barking\n" to the output
echo "Barking\n";
}
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->eat();
$dog->bark();
?>
Login to try C/C++/Java/PHP code in the editor
Inheriting Protected Members
parent::__construct() lets a child class's constructor explicitly call its parent's constructor, ensuring the inherited part of the object is initialized correctly before the child adds its own setup logic.
Example: Inheriting Protected Members
<?php
// Define the class `Animal`
class Animal {
// Declare the public property `$name`
public $name;
// Define the constructor `__construct` taking `$name`
function __construct($name) {
$this->name = $name;
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the constructor `__construct` taking `$name`
function __construct($name) {
parent::__construct($name);
}
}
// Create a new `Dog` instance with "Rex", stored in `$dog`
$dog = new Dog("Rex");
// Print `$dog->name` to the output
echo $dog->name;
?>
Login to try C/C++/Java/PHP code in the editor
The final Keyword
PHP only supports single inheritance for classes — a class can extend just one parent — though it can implement multiple interfaces, which is how PHP works around the ambiguity problems of true multiple inheritance.
Example: The final Keyword
<?php
// Define the interface `Swims` describing required methods
interface Swims {}
// Define the interface `Flies` describing required methods
interface Flies {}
// Define the class `Duck`, implementing `Swims, Flies`
class Duck implements Swims, Flies {
}
// Create a new `Duck` instance, stored in `$duck`
$duck = new Duck();
// Print a detailed dump (with types) of `$duck instanceof Swims`
var_dump($duck instanceof Swims);
?>
Login to try C/C++/Java/PHP code in the editor
Inheritance for Web Tools
A good inheritance relationship models a genuine is-a relationship, like Dog extends Animal, rather than being used just to reuse unrelated code, which tends to produce fragile, confusing class hierarchies.
Example: Inheritance for Web Tools
<?php
class Animal {}
class Dog extends Animal {}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
// Print a detailed dump (with types) of `$dog instanceof Animal`
var_dump($dog instanceof Animal);
?>
Login to try C/C++/Java/PHP code in the editor
- Overriding a method with a different signature that is incompatible with the parent, which gives a fatal error.
- Trying to inherit from a class or override a method marked
final. - Using inheritance for unrelated classes, when a class should only extend another when it is a kind of it.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: