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 :
- class ParentClass {
- public functionmyOwnMethod(){
- echo"ParentClass method called";
- }
- }
- class ChildClass extends ParentClass {
- public functionmyOwnMethod(){
- echo"ChildClass method called";
- }
- }
- functionrunClass(ParentClass $c){
- $c->myOwnMethod();
- }
- $c=newChildClass();
- 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);
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