Method Overloading is a concept of Object Oriented Programming which helps in building the composite application in an easy way. Function overloading or method overloading is a feature that permits making creating several methods with a similar name that works differently from one another in the type of the input parameters it accepts as arguments.
The above concept is fine for other programming languages and it is called static polymorphic i.e method overloading.
Example
Let's understand through an example.
Output:
Error
Explanation:
This will generate an error since php will say you have declared this method twice.
But Other programming languages says , doTask[$var1] and doTask[$var1,$var2] are overloaded methods. To call the latter, two parameters must be passed, whereas the former requires only one parameter.
so this behavior i.e decision to call a function at coding time is known as static polymorphic i.e method
overloading.
Let's discuss how to achieve method overloading related to PHP5.In the case of PHP, we have to utilize PHP's magic methods __call[] to achieve method overloading.
In PHP overloading means the behavior of method changes dynamically according to the input parameter. In this tutorial, we will understand those perceptions. Let's discuss the __call[] method.
__call[]:
If a class execute __call[], then if an object of that class is called with a method that doesn't exist then__call[] is called instead of that method.
Example
Let's understand method overloading with an example.
Output:
9.426 48
Explanation:
Here area[] method is created dynmically and executed with the help of magic method __call[] and it's behaviour changes according to pass of parametrs as object.
Updated on 31-Dec-2019 08:29:38
- Related Questions & Answers
- What is method overloading in C#?
- What is method overloading in Java?
- PHP Overloading
- What is runtime polymorphism or dynamic method overloading?
- What is the difference between method overloading and method hiding in Java?
- What is the difference between method overloading and method overriding in Java?
- What is overloading in C#?
- What is Overloading in Java?
- Method overloading in Java
- What is constructor overloading in Java?
- What is function overloading in JavaScript?
- What is overloading? What happens if we overload a main method in java?
- Function Overloading and Overriding in PHP
- Using Method Overloading in Java
- What is overloading a unary operator in C++?
PHP stands for Hypertext Preprocessor, it is a popular general-purpose scripting language that is mostly used in web development. It is fast, flexible, and pragmatic and the latest versions of PHP are object-oriented which means you can write classes, use inheritance, Polymorphism, Data Abstraction, Encapsulation, Constructor, Destructor, and as well as Overloading [Method and Function].
Overloading: Overloading is an Object-Oriented concept in which two or more methods have the same method name with different arguments or parameters [compulsory] and return type [not necessary]. It can be done as Constructor Overloading, Operator Overloading, and Method Overloading.
In this article, we will be implementing method overloading in PHP.
Method overloading in PHP:
Example: Given below code will be showing some error.
PHP
Output: The error is shown because we are redeclaring function multiply[] in GFG Class.
PHP Fatal error: Cannot redeclare GFG::multiply[]
Note: In other programming languages like C++, this will work for overloaded methods. To achieve method overloading in PHP, we have to utilize PHP’s magic methods __call[] to achieve method overloading.
__call[]: In PHP, If a class executes __call[], and if an object of that class is called with a method that doesn’t exist then, __call[] is called instead of that method. The following code demonstrates this.
Example:
PHP
Output:
35