Polymorphism in PHP with example in OOP

Spread the love

Polymorphism in PHP with example

What is Polymorphism in Object oriented PHP?

Polymorphism in PHP is one of the most important concepts in application development. Let’s see Polymorphism in PHP with example.

The word Polymorphism means many forms or many types.

In object oriented PHP’s Polymorphism principle, different classes can have the same method names which perform similar kind of operations but are different in the inherent process or functionality.

Confused?

Let’s see in detail.

The concept with the same name with different functionalities can be applicable to Shapes. Shapes like a square, rectangle, circle, etc are all having different length, breadth, height, radius, etc but all the shapes have an area.

Whatever functionality you do in order to calculate the area of these shape the result will always be called as an “Area” of a shape, for all the shapes.

So we can define a same-named method in all the shape classes, but the code to calculate the area of each shape can be different.

And when there is a common type of functionality in many classes we can use inheritance.

Let’s see how to create a Polymorphism in PHP with example?

To achieve polymorphism in PHP you can use abstract methods or interfaces.

As abstract methods and interfaces contain abstract methods which force the child classes to implement those methods, we can achieve polymorphism in classes by having the same method name.

Let’s see a Polymorphism in PHP with example:

<?php
 interface Shape
 {
  public function showArea();
 }
 
 class Square implements Shape
 {
     private $width;
  
  public function __construct($width)
  {
   $this->width = $width;
  }
  public function showArea()
  {
   $area = $this->width * $this->width;
   echo "Area of square = ".$area;
  }
 }
 
 class Rectangle implements Shape
 {
  private $width;
  private $height;
  
  
  public function __construct($width,$height)
  {
   $this->width = $width;
   $this->height = $height;
  }
  public function showArea()
  {
   $area = $this->width * $this->height;
   echo "<br>Area of rectangle = ".$area;
  }
 }
 
 $square = new Square(10);
 $square->showArea();
 
 $rectangle = new Rectangle(20,10);
 $rectangle->showArea();
?>

Output:

Area of square = 100
Area of rectangle = 200

In the above Polymorphism in PHP with example, we have created an interface Shape, which is having an abstract method showArea. And Shape interface is then implemented to Square and Rectangle classes.

This forces the classes Square and Rectangle to have and implement the method showArea in them.

Hence both the classes have a method with the same name but the internal code is different, as per the Square and Rectangle area calculation.

So whenever you want to call the function to calculate the area of a square or a rectangle, you can call the same function with a name showArea, but the calculation for both the shapes will be different in showArea.

In this way, you can achieve the polymorphism functionality in object oriented PHP programs.

I hope reading the Polymorphism in PHP with example helps you better understand the concept of polymorphism.

In the next tutorial, you will learn about static variables and static functions in detail.


Spread the love

Leave a Comment