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..

The following two tabs change content below.

Chandra Shekhar

GCP Architect
Chandra Shekhar Pandey is Google certified Cloud engineer, I am Magento2 Trained developer. Having huge experience in designing cloud solution. I have around 12 years of experience with world enterprise IT companies and fortune 500 clients. During my architecture design I am always caring about high availability, fast performance and resilient system. From the programmer background I have huge experience in LAMP stack as well. Throughout my carrier I have worked on Retail, E-Learning, Video Conferencing and social media domain. The motive of creating cutehits was just to share the knowledge/solutions I get to know during my day to day life so that if possible I can help someone for same problems/solutions. CuteHits.com is a really a very effort for sharing knowledge to rest of the world. For any query/suggestion about same you can contact me on below details:- Email: shekharmca2005 at gmail.com Phone: +91-9560201363

Latest posts by Chandra Shekhar (see all)

You may also like...