← Back to PHP Course | Chapter 8: OOP | Lesson 5 of 14

PHP Inheritance

Inheritance is like a child getting traits from a parent. A new class can automatically reuse everything from another class and then add its own extra skills.
Syntax
php
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
<?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();
?>

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
<?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();
?>

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
<?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;
?>

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
<?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);
?>

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
<?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);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Overriding a method with a different signature that is incompatible with the parent, which gives a fatal error.
  2. Trying to inherit from a class or override a method marked final.
  3. Using inheritance for unrelated classes, when a class should only extend another when it is a kind of it.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.