PHP Class Inheritance Tutorial

In this tutorial we learn how to create derivative child classes that inherit functionality from parent classes without having to rewrite their code.

What is inheritance?

Inheritance allows us to create a class which inherits functionality from another class. This is helpful when we want to create a class that extends the functionality of the original class without affecting it or any existing code that uses the original class.

Inheritance is usually referred to as an Is-A relationship. For example: a bicycle is a vehicle, a duck is an animal. The class that we inherit from is called the base class, super class, or parent class. The class that inherits functionality is called a sub class, or child class.

Inheritance allows us to reuse code from a parent class, inside a sub class. We wrap our common code in a parent class, then create a child class with only extended functionality. Inheritance also provides polymorphic behavior, which can be quite powerful.

How to inherit from another class

An existing class is ready to be inherited from, we don’t have to do anything special to it. This class is called the base class, super class or the parent class.

In PHP we use the extends keyword to indicate the class is inheriting from another class.

Syntax:
class ParentClass {

}

class ChildClass extends ParentClass {

}

The child class statement reads: ChildClass inherits from ParentClass.

Example:
<?php
    class Animal {
        var $name;

        function __construct($animalName) {
            $this->name = $animalName;
        }

        function move() {
            echo "<p>Animal parent class: All animals can move. " . $this->name . " is moving...</p>";
        }
    }

    class Dog extends Animal {

    }

    class Duck extends Animal {

    }

    $dog1 = new Dog("Fido");
    $dog1->move();

    $duck1 = new Duck("Donald");
    $duck1->move();

?>

In the example above we create two classes, Dog and Duck, that inherit from the Animal class. Both of the child classes have access to the same functionality and attributes that the parent class has.

The point of inheritance is to change or add functionality that isn’t available in the parent class. Let’s add some functionality unique to each child class.

Example:
<?php
    class Animal {
        var $name;

        function __construct($animalName) {
            $this->name = $animalName;
        }

        function move() {
            echo "<p>Animal parent class: All animals can move. " . $this->name . " is moving...<br>";
        }
    }

    class Dog extends Animal {
        function growl() {
            echo "Dog child class: A dog can growl but a duck can't. " . $this->name . " is growling...</p>";
        }
    }

    class Duck extends Animal {
        function quack() {
            echo "Duck child class: A duck can growl but a dog can't. " . $this->name . " is quacking...</p>";
        }
    }

    $dog1 = new Dog("Fido");
    $dog1->move();
    $dog1->growl();

    $duck1 = new Duck("Donald");
    $duck1->move();
    $duck1->quack();

?>

When we run the example above, both animals can move, so they still have access to the functionality in the parent class. They also have some functionality added that is unique to each of them.

The Duck won’t be able to use the growl() method and the Dog won’t be able to use the quack() method.

Summary: Points to remember

  • We can inherit functionality from another class without needing to rewrite it into our current class. This allows us to extend the functionality of a class without modifying it.
  • Inheritance provides polymorphic behavior which allows us to override the original functionality in the parent class to something more specifically related to the child class.
  • Inheritance is considered an Is-A relationship. A car is a vehicle.
  • The extends keyword is used when defining a class that inherits from another. ChildClass extends ParentClass.