LearnPolymorphism in PHP

Polymorphism in PHP

In this tutorial, we are going to learn about Polymorphism (Greek for "many forms") a naming convention that can help us write code which is much more coherent and easy to use. According to the Polymorphism 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 can be calculated by a method. The polymorphism principle says that, in this case, all the methods that calculate the area (and it doesn't matter for which shape or class) would have the same name.

For example, we can call the method that calculates the area calcArea() and decide that we put, in each class that represents a shape, a method with this name that calculates the area according to the shape. Now, whenever we would want to calculate the area for the different shapes, we would call a method with the name of calcArea() without having to pay too much attention to the technicalities of how to actually calculate the area for the different shapes. The only thing that we would need to know is the name of the method that calculates the area.

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;
  }
  
  // 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 method 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 implement 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

Conclusion

In this tutorial, we have learned how to implement the polymorphism principle in PHP. Click here to practice the subject. In the next tutorial, we will learn about type hinting that can restrict our functions and methods to use only arrays or objects that were created from a certain class.

comments powered by Disqus