PHP OOPs Concepts

Polymorphism in PHP

In this tutorial, we are going to learn a naming convention that can help us write code which is much more coherent and easy to use. According to the Polymorphism (Greek for “many forms”) principle, methods in different classes that do similar things should have the same name.

According to the Polymorphism principle, methods in different classes that do similar things should have the same name.

A prime example is of classes that represent geometric shapes (such as rectangles, circles and octagons) that are different from each other in the number of ribs and in the formula that calculates their area, but they all have in common an area that needs to be calculated. In such case, the polymorphism principle says that all the methods that calculate the area (and it doesn’t matter for which shape or class) should have the same name. For example, we can write in each class that represents a shape a calcArea() method that calculates the area with a different formula. Now, whenever the end users would like to calculate the area for the different shapes, the only thing that they need to know is that a method with the name of calcArea() is used to calculate the area. So, they don’t have to pay any attention to the technicalities of how actually each method works. The only thing that they need to know is the name of the method and what it is meant to do.

How to implement the polymorphism principle?

In order to implement the polymorphism principle, we can choose between abstract classes and interfaces.
In order to ensure that the classes do implement the polymorphism principle, we can choose between one of the two options of either abstract classes or interfaces.
In the example given below, the interface with the name of Shape commits all the classes that implement it to define an abstract method with the name of calcArea().

In accordance, the Circle class implements the interface by putting into the calcArea() method the formula that calculates the area of circles.

The rectangle class also implements the Shape interface but defines the function calcArea() with a calculation formula that is suitable for rectangles:

Now, we can create objects from the concrete classes:

We can be sure that all of the objects calculate the area with the method that has the name of calcArea(), whether it is a rectangle object or a circle object (or any other shape), as long as they inherit from the Shape interface. Now, we can use the calcArea() methods to calculate the area of the shapes:

Result :
28.274333882308
12

About the author

Anuj kumar

I'm Anuj kumar a web developer with 4+ year experience .I write blogs in my free time. I love to learn new technologies and share with others. I founded PHPGurukul in September 2015. The main aim of this website to is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Leave a Comment

1 Comment

Subscribe For Latest Updates