Back to Top

Understanding of Polymorphism in PHP

Understanding of Polymorphism in PHP

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:

  1. Method overloading
  2. 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:

The output is

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

The output is

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.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use Mixins in LESS CSS?

Posted on 9 years ago

Bhumi

Control Flow Functions in MySQL

Posted on 8 years ago

Bhumi

How to use Replace function in MySQL

Posted on 12 years ago

Bhumi

How to use Template Parameter in Joomla

Posted on 12 years ago

Bhumi