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().
1 2 3 |
interface Shape { public function calcArea(); } |
In accordance, the Circle class implements the interface by putting into the calcArea() method the formula that calculates the area of circles.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Circle implements Shape { private $radius; public function __construct($radius) { $this -> radius = $radius; } // The method calcArea calculates the area of circles public function calcArea() { return $this -> radius * $this -> radius * pi(); } } |
The rectangle class also implements the Shape interface but defines the function calcArea() with a calculation formula that is suitable for rectangles:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Rectangle implements Shape { private $width; private $height; public function __construct($width, $height) { $this -> width = $width; $this -> height = $height; } // calcArea calculates the area of rectangles public function calcArea() { return $this -> width * $this -> height; } } |
Now, we can create objects from the concrete classes:
1 2 |
$circ = new Circle(3); $rect = new Rectangle(3,4); |
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:
1 2 |
echo $circ -> calcArea(); echo $rect -> calcArea(); |
Result :
28.274333882308
12