Overloading in PHP with Example

No Comments

Function overloading or method overloading is a feature that allows creating several methods with the same name which differ from each other in the type of the input parameters. It is simply defined as the ability of one function to perform different tasks. For example, doTask() and doTask(object O) are overloaded methods. To call the latter, an object must be passed as a parameter, whereas the former does not require a parameter, and is called with an empty parameter field. It is a form of static polymorphism which .The decision to call an implementation or another is taken at coding time.

Lets take the famous area example which will calculate the area depending on the number of inputs given:


This is erroneous  since php will say you have declared this method twice . Other languages says its fine and calls it static polymorphic i.e method overloading. You have to use PHP’s magic methods to achieve method overloading in PHP.


Leave a Reply

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