PHP OOP Polymorphism Tutorial

In this tutorial we learn how to redefine the methods of a parent class inside a child class by overriding them.

We also cover how to access a parent method in an overridden child with the double colon (::).

What is polymorphism?

The word polymorphism means “having many forms”. Polymorphism enables a child class to define its own version of a member that’s also defined by its parent class. We change the implementation of an inherited member.

Simply put, we override any method that exists in a parent class with our own functionality in the child class.

How to override a parent class method

To override a method in a parent class, we simply need to create a method the child class with the exact same name.

Syntax:
class ParentClass {
    function methodName(){
    }
}

class ChildClass {
    function methodName() {
    }
}

We cannot override a method in a parent class if the method, or the class itself, is marked as final. We learn more about the final modifier in the tutorial lesson on encapsulation.

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>";
        }

        function move() {
            echo "<p>Dog child class: Overriding the move() of the parent class. " . $this->name . " is walking...<br>";
        }
    }

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

        function move() {
            echo "<p>Duck child class: Overriding the move() of the parent class. " . $this->name . " is waddling...<br>";
        }
    }

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

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

?>

When we run the example above, each child class shows a different message when their move() method is called because we override it in both of them.

How to access a parent method in an overridden child method using the double colon (::)

If we override a parent method in a class, we can still access the original parent method. To do this we have to use the scope resolution operator, otherwise known as the double colon (::).

We covered the double colon briefly when we explained how to access class members directly with the double colon .

Let’s say, as an example, that we don’t want to override all the functionality in a parent class method, we just want to add to it.

We can override the method, implement our own functionality, then call the parent method inside our child method. This allows the child method to contain the functionality of the parent, along with our custom functionality.

Example:
<?php

    class ParentClass {

        function method() {
            echo "<p>I'm a method in the ParentClass</p>";
        }
    }

    class ChildClass extends ParentClass {

        function method() {
            echo "<p>Overloading method() from the parent</p>";

            ParentClass::method();
        }

    }

    $child1 = new ChildClass();
    $child1->method();

?>

In the example above, we override method() in the child class, providing our own behavior for it. We also call method() from the parent class with the double colon (::), which will execute the original parent method’s behavior.

This way, we have custom our functionality on top of the original functionality.

We don’t have to specify the class name explicitly. We can substitute the class name with the keyword parent .

Example:
<?php

    class ParentClass {

        function method() {
            echo "<p>I'm a method in the ParentClass</p>";
        }
    }

    class ChildClass extends ParentClass {

        function method() {
            echo "<p>Overloading method() from the parent</p>";

            parent::method();
        }

    }

    $child1 = new ChildClass();
    $child1->method();

?>

Abstract classes & Interfaces

Both abstract classes and interfaces are also polymorphic. We cover abstract classes in the lesson on encapsulation and interfaces in the lesson on interfaces .

Summary: Points to remember

  • Polymorphism is, essentially, overriding a the behavior of a method in a parent class.
  • A parent method is overridden by simply giving it the same name in the child method.
  • When we want to reference a method from a parent class inside of an overridden method in a child class we use the double colon (::).