Method Overriding

Breathing Fresh Life into Logic with Method Overrides

PHP’s object oriented principles allow for both method overloading and method overriding. You might have heard both of those terms, but don’t know what they mean, so I want to help to differentiate them for you.

Method overloading is when you have two methods with the same name but a different number of parameters. In programming languages like Java, this is pretty simple to achieve. You create two methods with the same name and you provide them with a different number of arguments.

  • area(diam)
  • area(length, width)

In the example above, we have a different number of arguments to calculate the area of a circle vs calculating the area of a rectangle.

In PHP, you have to use the magic function __call(), which is a topic of another article. We’re discussing method overriding in this one.

If you’ve been following along with this article series, you might remember that we created a Dog class with a bark() method. We inherited our Dog class inside of our GermanShepherd class and by association, we inherited all of the methods and properties, including the bark() method.

We wanted our GermanShepherd bark to be different, so we created a bark() method inside of our GermanShepherd class and that bark() method overrode the bark() method that was inherited from the Dog class.

<?php

class Dog {
  
  bark() {
      echo "I will get overriden.";
  }
}

class GermanShepherd extends Dog {
  
  bark() {
      echo "I inherited the bark method from Dog, but I'm overriding it.";
  }
}

To override an inherited method, you just have to make sure that the method name, and the number of arguments, are exactly the same.

https://blog.devgenius.io/php-p55-file-separation-c3e848295e8d

Continuing from where we left off, we have each of the classes separated into its own individual file. We’re going to create a new file, called MethodOverriding.php, to show our bark() method in action.

<?php
require_once("GermanShepherd.php");

$gs_dog = new GermanShepherd("Black", "Oct 15, 2018", "Brown");
echo $gs_dog->bark() . "<br>";
echo $gs_dog->walk() . "<br>";

The call to the bark() method outputs “Loud Barking,” since that was the output that we specified in the bark() method in the GermanShepherd class.

As a refresher, our GermanShepherd and Dog classes contain the following methods.

<?php
require_once('Mammal.php');

class Dog extends Mammal
{
    //...

    public function bark()
    {
        echo "I'm barking";
    }
}
<?php
require_once("Dog.php");

class GermanShepherd extends Dog
{

    //...

    public function bark()
    {
        echo "Has heart: " . self::HAS_HEART;
        echo "Loud Barking";

    }
    
    //...
}

Let’s override some other methods, like walk(). To make it more interesting, we’ll add the walk() method to our Mammal class as well. This way, the Dog class inherits from Mammal and overrides the Mammal walk() method, and the GermanShepherd class inherits from Dog and overrides the Dog walk() method. But one step at a time.

First, let’s only add the walk() method to our Mammal class and comment out the walk() method from our Dog class. So that it’s easier to visualize, I’m throwing all of the code into one file, but you should keep your files separate.

<?php
class Mammal extends Animal {

    //...

    public function walk(): string
    {
        return "I'm a walking Mammal";
    }
}

class Dog extends Mammal
{
    //..

//     public function walk(): string {
//         return "I'm a walking dog";
//     }

    //..
}

class GermanShepherd extends Dog
{
    //...
}

When we instantiate our GermanShepherd class and call walk(), we get I’m a walking Mammal. Next, let’s uncomment the walk() method from the Dog class so that it overrides the Mammal walk() method.

<?php
class Mammal extends Animal {

    //...

    public function walk(): string
    {
        return "I'm a walking Mammal";
    }
}

class Dog extends Mammal
{
    //..

    public function walk(): string {
        return "I'm a walking dog";
    }

    //..
}

class GermanShepherd extends Dog
{
    //...
}

If we call the walk() method again, we get I’m a walking dog. This is because the Mammal walk() method was overridden.

<?php
require_once("GermanShepherd.php");

$gs_dog = new GermanShepherd("Black", "Oct 15, 2018", "Brown");
echo $gs_dog->bark() . "<br>";
echo $gs_dog->walk() . "<br>";

Finally, let’s create a walk() method inside of our GermanShepherd class that returns the string I’m a walking GermanShepherd.

Calling our GermanShepherd walk() method now outputs I’m a walking German Shepherd. The GermanShepherd walk() method overrode the Dog walk() method, which overrode the Mammal walk() method.

That only works for methods that are inherited, which are public and protected methods. Private methods aren’t inherited, so they’re not overridden. The public or protected visibility modifiers must be kept exactly the same between the parent and the child class for overriding to work.

Pretty simple.

File Separation

PIECING TOGETHER MASTERPIECES WITH INCLUDED FILES

PHP – P55:FILE SEPARATION

Include, Require, Require Once, and Include Once. You might have seen these at the top of a file in someone else’s code, but what does each one of them mean?

Method Overriding

Breathing Fresh Life into Logic with Method Overrides

PHP – P56:method overriding

PHP’s object oriented principles allow for both method overloading and method overriding. But what do these terms mean?

Double Colon

ASCENDING LOGIC PEAKS WITH THE RESOLUTION OPERATOR

PHP – P57:SCOPE RESOLUTION

In PHP, the scope resolution operator, or the double colon ::, allows you to access constants, static properties & methods, and overridden properties & methods.

Leave a Reply