Method Overloading (Function Overloading) in PHP
Function overloading in PHP is not similar to other Programming language. Function overloading is also known as method overloading.
Method overloading in PHP is supported by using two magic functions
i) __call : This magic method gets called automatically when someone call any inaccessible method with the object.
ii) __callstatic : This magic method gets called automatically when someone call any inaccessible method with static keyword.
For Example:
<?php /* * * */ Class Method_Overloading { /* * This magic method will get called if someone * will call any method which is not avialable * within the class.And try to call it through * object of class * */ public function __call($name,$parameter) { echo "<br>calling __call function<br>"; } /* * This magic method will get called if someone * will call any method which is not avialable * within the class.And try to call it through * without object of class i.e., with direct calling. * This magic method is only available in PHP 5.3.0 and above * */ public static function __callstatic($name,$parameter) { echo "<br>calling __callstatic function "; } } $objMethodOverloading = new Method_Overloading(); // calling inaccessible method with object $objMethodOverloading->newFunction("new function calling"); // calling inaccessible method without object Method_Overloading::newFunction("lets call");
See the output:-
calling __call function calling __callstatic function
In the next blog you will see Object Overloading. I Hope you have enjoyed otherwise you can reach to me through your comments..
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments