PHPGurukul

Polymorphism in PHP

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().

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.
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:
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:
$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:
echo $circ -> calcArea();
echo $rect -> calcArea();
Result : 28.274333882308 12]]>

Anuj Kumar

This is Anuj Kumar. I’m a professional 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 provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you


2 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Web Hosting with a FREE domain. On sale ₹99.00/mo

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.