PHP OOP – basic concept of Polymorphism

PHP OOP – basic concept of Polymorphism

In this post we will give you information about PHP OOP – basic concept of Polymorphism. Hear we will give you detail about PHP OOP – basic concept of PolymorphismAnd how to use it also give you demo for it if it is necessary.

Polymorphism is basically derived from the Greek which means ‘many forms’.
In other words, polymorphism is what join bunch of classes with one interface. polymorphism is a key part of php oops.
As you can see in above image, printer class has a print method which performs multi task, such as print to screen and print to paper.
Also, you can take example of twin brothers, they look like same but have different characters.

 Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.

Here is a example which tell you about polymorphism : 

  1. class ParentClass {
  2. public functionmyOwnMethod(){
  3. echo"ParentClass method called";
  4. }
  5. }
  6. class ChildClass extends ParentClass {
  7. public functionmyOwnMethod(){
  8. echo"ChildClass method called";
  9. }
  10. }
  11. functionrunClass(ParentClass $c){
  12. $c->myOwnMethod();
  13. }
  14. $c=newChildClass();
  15. runClass($c);
class ParentClass { public function myOwnMethod() { echo "ParentClass method called"; }}class ChildClass extends ParentClass { public function myOwnMethod() { echo "ChildClass method called"; }}function runClass(ParentClass $c) { $c->myOwnMethod();}$c = new ChildClass();runClass($c);

Label :

OOP

PHP

Object Oriented Programming

Web Development

Hope this code and post will helped you for implement PHP OOP – basic concept of Polymorphism. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  23  =  24

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US