Object-oriented programming (OOP) is a fundamental characteristic of programming language and Object-oriented programming (OOP) of PHP5 provides good support. How to use OOP ideas for high-level programming of PHP, PHP programming for improving the capacity and framework for Web development is very significant.
In this post, I am going to explain about Polymorphism ,different types of polymorphism PHP Supports and How to implement Polymorphism in PHP.Let’s start with the definition of Polymorphism.
What is polymorphic?
Polymorphism is the most important in OOPs.Polymorphism specializes the behaviour of a derived data types means the behaviour of base class meets the requirements of derived class.
Polymorphism is a variety of forms, also known as “one external interface, multiple internal implementation method”.Polymorphism allows each object to fit their own way to respond to a common message. Polymorphism enhances the flexibility and reusability of the application.
Polymorphism Types:
- Method overloading
- operator overloading
Overloading is compile time polymorphism. The compiler decides which method form to use based on the method signature. Overwriting is true run time polymorphism.
PHP supports polymorphism by allowing subclasses instance to be used in place of parent instances.PHP doesn’t support Method overloading as a built-in feature.Method overloading encloses methods with different numbers of arguments.
Suggested Read: How to use Interface in PHP.
A pointer to the base class type can be referred to an object of derived class type is called polymorphism.
In Simple Words,
Poly means “many” and morph means “form”
When two classes implement the same external methods, they should be able to be used interchangeably in functions.
Let’s understand by code:
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 | class Triangle{ { echo "Triangle\n"; } } class Square{ function volume() { echo "Square\n"; } } function printBox($obj) { if ($obj instanceof Triangle) { $obj->area(); } else if ($obj instanceof Square) { $obj->volume(); } else { } } printBox(new Triangle()); printBox(new Square()); |
The output is
1 2 3 4 5 | // Output Triangle Square |
In above example, If you want to define more boxes you need to extend function of class so you check that the object is an instance of one of those class and then you have to add the code to call each method and to solve this problem you need to go with Inheritance.
Inheritance enables to inherit from a parent class inheriting all its methods and properties and create an is-a relationship.
Let’s create new class of above example
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 | class Box { function BoxFn() { } } class Triangle extends Box { function BoxFn() { echo "Triangle\n"; } } class Square extends Box { function BoxFn() { echo "Square\n"; } } function printBox($obj) { if ($obj instanceof Box) { $obj->BoxFn(); } else { echo "Error occured"; } } printBox(new Triangle()); printBox(new Square()); |
The output is
1 2 3 4 5 | //Output Triangle Square |
Polymorphism in PHP is the interpretation of language which is very easy and natural.The concept of polymorphism a little fuzzy but once you are clear with concept, you can develop any application.
Hope this article is helpful to someone. If you have any query,feel free to ask in the comment section.