Polymorphism in PHP is one of the important concepts in OOPs.
Polymorphism is a Greek word. Polymorphism is created by two different words poly (means many) and morph (means forms).
It is another functionality of OOPS. In a programming language, two types of Polymorphism are there.
- Function overriding (Run time Polymorphism)
- Function Overloading(Compile time Polymorphism)
Function Overriding in PHP
Overriding happens only in child class.
If the parent class declares a function and the child class wants to override the parent class function then function overriding means run time polymorphism happened.
Polymorphism example in PHP
Here are taking a simple example.
Created a class parent class it has a method name().
created another class child class it inherits the feature of the parent class.
child class has also the same method name().
Defining the same method in a child is known as method overriding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php class parentclass { function name() { return 'Calling Parent name()'; } } class childclass extends parentclass { function name() { return 'Calling Child name()'; } } $obj = new childclass(); echo $obj->name(); echo '<hr>'; $arrobj = array(); $arrobj[0] = new parentclass; $arrobj[1] = new childclass; foreach ($arrobj as $key => $val) { echo $val->name(); echo "<br> "; } //It called child function instead of parent function ?> |
Output
Calling show method of Shape Shape is no defined Calling show method of Circle Showing a Circle Calling show method of Rectangle Showing a Rectangle Calling show method of Square Showing a Square Calling show method of Triangle Showing a Triangle
Polymorphism example to call different methods in PHP
Here we have taken the example of Shapes.
The shape is a base class with a method shown ().
Child classes of Shape are Circle, Rectangle, Square, and Triangle.
all have an overridden method show().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | <?php class Shape { function show() { echo 'Shape is no defined<br/>'; } } class Circle { function show() { echo 'Showing a Circle<br/>'; } } class Rectangle { function show() { echo 'Showing a Rectangle<br/>'; } } class Square { function show() { echo 'Showing a Square<br/>'; } } class Triangle { function show() { echo 'Showing a Triangle<br/>'; } } //creating Shape object $objShape = new Shape(); //calling Shape method with shape object echo ' Calling show method of Shape<br/>'; $objShape->show(); //creating Circle object $objCircle = new Circle(); //calling Circle method with shape object echo ' Calling show method of Circle</br>'; $objCircle->show(); //creating Rectangle object $objRectangle = new Rectangle(); //calling Rectangle method with shape object echo ' Calling show method of Rectangle</br>'; $objRectangle->show(); //creating Square object $objSquare = new Square(); //calling Square method with shape object echo ' Calling show method of Square</br>'; $objSquare->show(); $objTriangle = new Triangle(); //calling show method with shape object echo ' Calling show method of Triangle</br>'; $objTriangle->show(); ?> |
Output
Calling show method of Shape Shape is no defined Calling show method of Circle Showing a Circle Calling show method of Rectangle Showing a Rectangle Calling show method of Square Showing a Square Calling show method of Triangle Showing a Triangle