PHP and MySQL (34 Blogs) Become a Certified Professional

How To Implement Polymorphism In PHP?

Published on Aug 20,2019 21 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Poly means many and Morphism means forms or types. Using a single object in multiple ways is known as Polymorphism. In other words, a function playing a different role for different scenarios. In this article we will be exploring Polymorphism in PHP

Following pointers will be covered in this article

  • Polymorphism using Interface
  • Polymorphism using Abstract Class

So let us get started with this article,

Polymorphism in PHP

Polymorphism using Interface

Interface is similar to the class but inside the interface only the functions are declared. It doesn’t have any body or functionality in the interface. Interface needs to be implemented by a concrete class. Implementing class has to override all the functions that are defined in an interface


<?php
interface vehicle
{
public function choose();
}
class MyBike implements vehicle
{
public function choose()
{
echo "Bike choosed". "<br>";
}
}
class MyCar implements vehicle
{
public function choose()
{
echo "Car choosed" ;
}
}
$obj=new MyBike();
$obj->choose();
$obj1=new MyCar();
$obj1->choose();
?>

Output

Bike choosed

Car choosed

Moving on with this Polymorphism In PHP article

Polymorphism using Abstract Class

Abstract class is a class which is only used to inherit a class from it. It can be used only as a parent class. In other words you cannot declare an object of the class which is declared as abstract


<?php
abstract class shape
{
abstract protected function area();
}
class rectangle extends shape
{
var $l,$b;
public function __construct($x , $y)
{
$this->l=$x;
$this->b=$y;
}
public function area()
{
$a=$this->l*$this->b;
return $a;
}
}
class circle extends shape
{
var $r;
public function __construct($x)
{
$this->r=$x;
}
public function area()
{
$pi=3.142;
$a=pow($this->r,2)*$pi;
return $a;
}
}
$r1=new rectangle(10,20);
$area=$r1->area();
echo "rectangle area= $area <br>";
$c1=new circle(10);
$area=$c1->area();
echo "cirlce area=$area<br>";
?>

Output

rectangle area= 200

circle area=314.2

With this we come to an end of this article, I hope you understood the concept of polymorphism and how to obtain Polymorphism using Interface as well as Abstract Class. 

If you found this blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”Polymorphism in PHP” and I will get back to you.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.