In this article, we are going to discuss polymorphism in php which is a term defined used in writing code and it is very simple to use.

What is polymorphism ?

Polymorphism principle define that, in a programming language, there are different classes consist of methods that do same things and it is important that it should have the same name.

We are going to check this polymorphism principle by using example.

Polymorphism plays an important role in the OOP concept.

It helps different classes having different functions to execute and share interface.

The main important thing regarding polymorphism is the code which is written in various classes.

The code does not effect because method doing the same thing and have have same.

How to implement polymorphism in php?

In order to implementing this polymorphism principle we are going in a detail step by step.

Let’s start with the implementation of polymorphism principle using interface.

So before starting with polymorphism, we discuss regarding some important points of interface.

Interface is a similar to class which consist of method which is not defined and it does not consist of code.

An Interface is consist of methods name and arguments but there is not any content in the method.

Below is the example of polymorphism from which you can understand in a easy way.

<?php interface Bicycle { public function calcMeasurement(); } class Circle implements Bicycle { private $radius; public function __construct($radius){ $this -> radius = $radius; } public function calcMeasurement(){ return $this -> radius * $this -> radius * pi(); } } class Rectangle implements Bicycle { private $width; private $height; public function __construct($width, $height){ $this -> width = $width; $this -> height = $height; } public function calcMeasurement(){ return $this -> width * $this -> height; } } $circlemeasurement = new Circle(3); $rectanglemeasurement = new Rectangle(3,4); echo $circlemeasurement->calcMeasurement(); echo $rectanglemeasurement->calcMeasurement();?>



In the above example you can see there is an interface with the name Bicycle

Which commits all the classes that implement it to do define with a method with the name calcMeasurement().

In accordance, the Circle class implements the Bicycle interface by putting into the calcMeasurement() method the formula that calculates circles measurement.

And below the circle class if you see there is rectangle class.

The rectangle class also implements the Bicycle interface but defines the method calcMeasurement() with a calculation formula that is suitable for rectangles.

Now the next step is to create objects from the classes.

$circlemeasurement = new Circle(3);$rectanglemeasurement = new Rectangle(3,4);

Objects calculate the measurement with the method that has the name calcMeasurement(), whether it is a rectangle object or a circle object, as long as they implement the Bicycle interface.

And finally calcMeasurement() methods to calculate the measurement of the Bicycle

echo $circlemeasurement->calcMeasurement();echo $rectanglemeasurement->calcMeasurement();

Output :



Finally we done with the point of what is polymorphism in php.

How to fetch image from database in php

I hope you liked my this article. If you have any queries or any question regarding this, Feel free to comment on Me.