Polymorphism is one of the good concepts of the PHP Object Oriented Programming Language features (OOPS features). The word polymorphism is actually derived from the two Greek words “Poly” and “Morph”. The meaning of poly is “many” and morph is “forms”. It means the ability to have many forms. In common terms, Polymorphism is an Object-Oriented Programming pattern in which the class is having various functionalities while having common interfaces. Actually Polymorphism is of two types: Compile-Time and Run-Time. PHP doesn’t support compile-time polymorphism which means function and operator overloading.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Class a{ //methods Public function b(){ //methods } } Class b extends a{ //methods Public function b(){ //methods which are in/not in the function of the class a. } }
Explanation:
In the above syntax, we made 2 classes one is normal and the other one is to extend the class but we used the same function name/method for different functionalities. This is called poly morphing. One can use many similar names of functions/methods with different functionalities as needed in the coding.
PHP’s Polymorphism works based on the run time polymorphism concept. In PHP, Polymorphism works by making the decision at runtime which is actually not a compile-time polymorphism concept or else we can say that we are having many/multiple implements which are subtype for a super class, runtime (function overloading) concept is an example of polymorphism of PHP Programming Language.
Function overloading means deriving a class from a newly created function with the same signature (it means the function is having the same name, same type and a number of arguments) in its parent class as a function which is called as method overriding. In this polymorphism concept, one can create functions by extending but the same name/ similar naming function should be involved in order to process the polymorphism and inheritance concept. In order to enhance the programming capability by increasing the class’s functionalities which having the common interfaces polymorphism is a must.
Below are the examples of Polymorphism in PHP:
In the below example, one of the base class “Shap1” is created. Here we are going to inherit the Shap1 class in the three derived classes. The classes names are Circle1, Triangle1, and Elliipse1. Each and every class has the function draw in order to complete the runtime Polymorphism of PHP programming language. In the following calling process, here we are going to create an array with length 2 and every array’s index is helpful in creating an object of one class. After this, we are going to use a loop that is going to be executed based on the length of the array which is having a value “$i1” which is passing to the object array variable “$Val1[$i1]”. So it will be executed 3 times and it will be called using draw() method of each and every class.
Code:
<?php class Shap1 { function draw1(){} } class Circle1 extends Shap1 { function draw1() { print "Circle1 has been drawn.\n"; } } class Triangle1 extends Shap1 { function draw1() { print "Triangle1 has been drawn.\n"; } } class Ellipse1 extends Shap1 { function draw1() { print "Ellipse1 has been drawn."; } } $Val1=array(2); $Val1[0]=new Circle1(); $Val1[1]=new Triangle1(); $Val1[2]=new Ellipse1(); for($i1=0;$i1<3;$i1++) { $Val1[$i1]->draw1(); } ?>
Output:
This is the example of the implementation of the Polymorphism concept with the interface help. Interface/interfaces are/are very similar to any class except which is not containing any code. The interface helps in defining the method names and also method arguments but here contents of the methods will not be defined. The interface which is executing by any class/classes must be executed by all the methods which are characterized by the interface.
Here Machine1 will have all classes to define calcTask1. Circle1 implements CallTask1() method. Rectangle1 also tries to implement a Machine interface but defines with a different body of calcTask1() method. Polymorphism says that all the methods which calculate the task should have an equivalent name. We are going to call the different names of calcTask1() without providing the details. Here the name of the method which calculates the task is important.
Code:
<?php interface Machine1 { public function calcTask1(); } class Circle1 implements Machine1 { private $radius1; public function __construct($radius1){ $this -> radius1 = $radius1; } public function calcTask1(){ return $this -> radius1 * $this -> radius1 * pi(); } } class Rectangle1 implements Machine1 { private $width1; private $height1; public function __construct($width1, $height1){ $this -> width1 = $width1; $this -> height1 = $height1; } public function calcTask1(){ return $this -> width1 * $this -> height1; } } $mycirc1 = new Circle1(3); $myrect1 = new Rectangle1(3,4); echo $mycirc1->calcTask1(); echo $myrect1->calcTask1(); ?>
Output:
In the below example a class “base11” is created with a function add1() with $a1 and $b1 variables. Then “$res1” variable is created which is used to store the multiplication of the two variables “$a1” and “$b1” and the echo statement is used to print if the function is called. Then Child1 class is used to extend the base11 class. In the extended class, again add1() variable is created to store the sum or the a1 and b1 variables. Then a new object is created and called by passing the variable values to the values $a1 and $b1 variables.
Code:
<?php class base11 { function add1($a1,$b1) { $res1=$a1*$b1; echo "Multiplication = ".$res1; } } class child1 extends base11 { function add1($a1,$b1) { $res1=$a1+$b1; echo "Sum = ".$res1; } } $obj= new child1(); $obj->add1(1000,500); ?>
Output:
In the below example a class “BaseClass11” is created with the public function “myMethod()” is created with the echo statement to mention that “BaseClass11 method” is called that. Then again DerivedClass11 is created to extend the BaseClass11 with the same name function “myMethod()” with the echo statement to print “DerivedClass11 method called” like that. We are calling the same/similar naming function in different classes. Then new object is created for the function/ method “myMethod()”.
Code:
<?php class BaseClass11 { public function myMethod11() { echo "BaseClass11 method called"; } } class DerivedClass11 extends BaseClass11 { public function myMethod11() { echo "DerivedClass11 method called"; } } function processClass11(BaseClass11 $c11) { $c11->myMethod11(); } $c11 = new DerivedClass11(); processClass11($c11); ?>
Output:
This is a guide to the Polymorphism in PHP. Here we discuss the what is the definition of Polymorphism and its Working along with its examples as well as Code Implementation. You can also go through our other suggested articles to learn more-
The above is the detailed content of Polymorphism in PHP. For more information, please follow other related articles on the PHP Chinese website!