
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
What is method overloading in PHP?
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.
<?php class machine { function doTask($var1){ return $var1; } function doTask($var1,$var2){ return $var1 * $var1 ; } } $task1 = new machine(); $task1->doTask(5,10); ?>
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.
<?php class Shape { const PI = 3.142 ; function __call($name,$arg){ if($name == 'area') switch(count($arg)){ case 0 : return 0 ; case 1 : return self::PI * $arg[0] ; case 2 : return $arg[0] * $arg[1]; } } } $circle = new Shape(); echo $circle->area(3); $rect = new Shape(); echo $rect->area(8,6); ?>
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.
- Related Articles
- What is method overloading in Java?
- What is method overloading in C#?
- PHP Overloading
- What is runtime polymorphism or dynamic method overloading?
- What is the difference between method overloading and method hiding in Java?
- Method overloading in Java
- Function Overloading and Overriding in PHP
- What is overloading? What happens if we overload a main method in java?
- Using Method Overloading in Java
- What is overloading in C#?
- What is Overloading in Java?
- What are the restrictions placed on method overloading in java?
- What is constructor overloading in Java?
- What is function overloading in JavaScript?
- Difference between Method Overloading and Method Overriding in Java
