Classes and Objects
Overloading Methods

Sometimes, we want to change how methods behave for subclasses from the original parent definition. This is called overloading a method. To do this, define a new method within the subclass with the same name as the parent method.

For example, our Pet class might have a type method:

class Pet { function type() { return "pet"; } }

But in our Dog class, we want to update this message:

class Dog extends Pet{ function whatIsThis() { return "dog"; } }

We can call the parent’s definition of the method within the subclass using parent:: followed by the method name:

class Dog extends Pet{ function type() { return "dog"; } function classify(){ echo "This " . parent::type() . " is of type " . $this->type(); // Prints: This pet is of type dog } }



We’ve added the Milk class from the last exercise. Now we want to update the getInfo method to make it clear that we like our milk cold.

Overload this method to print "This beverage is <temperature>. I like my milk this way.", with <temperature> replaced with the value from the object’s property. Re-use the parent’s implementation of the method within the new definition.


Test the method by creating an instance of Milk and print the result of calling getInfo on it.

Folder Icon

Take this course for free

Already have an account?