Understanding Polymorphism in PHP

Ojo Joseph Oluwaseun
Apr 4 · 4 min read

When it comes to Object Oriented Programming, Polymorphism is indeed a wonderful and powerful tool you need to get fully acquainted with but if you’re relatively new to OOP, the word Polymorphism alone might scare you a little bit but the truth is Polymorphism is just a long word for a very simple concept and I strongly believe you will grab how it works after the end of this minimal tutorial as this tutorial will describe the general concept of polymorphism, and how you can easily apply it in PHP. Enough of the talk, what is Polymorphism?

First, it is derived from the Greek words Poly (meaning many) and morph (meaning forms)

Polymorphism is simply an OOP design pattern that allows multiple class with various functionalities to implement or share a common Interface.

The aesthetic of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way.

Polymorphism in PHP is implemented using the interface pattern. Think about interface as the skeleton or blueprint for your class which implements it.

interface shapesInterface{

}

The above code has an interface named shapesInterface.

An interface is similar to a class except that it cannot contain code but only method declaration and this declaration can only work with the public scope as seen below:

interface shapesInterface{
public function getArea();
}

An interface can define method names and arguments, but not the contents of the methods. Any class implementing an interface must implement all methods defined by the interface. Below are the main points of Interface:

  1. An interface is defined to provide a common function name to the implementer classes.
  2. Different implementing classes can implement those interfaces according to their requirements.
  3. You can say, interfaces are skeletons which are implemented by developers.
// Below is an example of what point 1 denotes:class Rectangle implements shapesInterface{  public function getArea(){    return "Rectangle Area";  }}

As we can see from the example above class Rectangle is implementing the shapesInterface we created earlier and it has the same common method name as the shapesInterface (getArea) except that the interface method has no code in it.

//First things first, define the interfaceinterface shapesInterface{    //First method    public function getArea();   //Second method

public function totalWidthSize();
}/*
|Now that we've created our interface, Take a good look at the differences between the two classes below
*/
//Invalid and will output a fatal error
class Rectangle implements shapesInterface{
public function getArea(){ }}//Valid and will work just fine
class Rectangle implements shapesInterface{
public function getArea(){}public function totalWidthSize(){}}

When implementing an interface, a class must call all methods in the interface it's implementing as seen above, when the first class implementation is rendered, a fatal error will occur because it's not implementing all the methods in shapesInterface.

2. Different implementing classes can implement those interfaces according to their requirements.

class Polygon implements shapesInterface{public function getArea(){return "Polygon Area";}public function totalWidthSize(){return "Total width Size";}}

As seen above, while Rectangle is implementing shapesInterface, other classes can as well do the same as the Polygon class above but they all have to follow the interface architecture.

Another instance where Polymorphism can prove really helpful in PHP is when you protect a function by passing in a class name while passing an object. Let us take a look at the example below:

/*
|Create a function that lets you calculate the area of a rectangle Using the Rectangle class above
|Using the class name Rectangle will only allow class Rectangle object to be passed which is really a security measure so nobody passes any unwanted class object into the function
*/
function calculateRectangle(Rectangle $object){ // return $object->getArea();}

The example above is just a regular thing in OOP, but what if we want other classes aside from Rectangle to be passed as a parameter to our function calculateRectangle above, all we have to do is wrap them all in an interface and just call the interface name as seen below:

//PHP will clearly understand that this is an interface and we want only the class in this interface to be passed into this method belowfunction calculateRectangle(shapesInterface$object){// return $object->getArea();}

In conclusion, Polymorphism describes a pattern in Object Oriented Programming in which a class has varying functionality while sharing a common interfaces.

I hope you now understand what Polymorphism is and how you can apply it in PHP. Have a nice day!

Ojo Joseph Oluwaseun

Written by

I’m Joseph, I have over 4 years of experience building Web Applications, Middleware technology, user interface design and Consulting.