Function Overloading in PHP

It is overloading in PHP. Function overloading or method overloading is a feature that creates various methods with the same name that varies in the type of input parameters. It is simply described as the sense of one function to execute various tasks.

Definition of overloading in a single line:

. “Same Name But Different Implementations“.

How does Overloading work in PHP?

As we know, method overloading is made on functions. Therefore functions can be overloaded with various implementations based on the arguments given to each function. For example to find the area of a circle which is pi ( π) *r *r radius is given, to get the area of square which is side * side, length of the side is given, area of a triangle is (base * height) / 2 the base length and the height is provided and so on.

Overloading Concepts.

Magic Methods start with (double underscore) __and get called automatically by PHP. Also, they are always described inside the class and not outside the class. Some magic methods are , __destruct(), __call(), __callStatic(), __get(), __set,__construct(), __isset(), __unset(),

Overloading when executed creates properties and methods dynamically which is mutually called entities.

  • properties: data members
  • methods: data functions

Let’s look at how the magic design works using the __call() method.

<?php
class SOFT{
private $a = array('seven','eight','six');
public function__call($method, $args) {
print "Method $method is called:\n";
echo "<pre>";
print_r($args);
return $this->a;
}}
$obj = new SOFT();
$a = obj->anyMethod(1, 2, 3);
var_dump($a);
?>

In the above case, we have published a class called Test which has private property and a __call method. Two arguments are passed to the __call method. Inside the __call method, we have used statement print(), a var_dump the arguments, and a value x are returned.

Then the object of the class with the name obj is organized and using this object an unclear method is called since this method doesn’t exist __call() is called instead.

Types of Overloading in PHP

  1. Property Overloading.
  2. Method Overloading.

Method Overloading

Method overloading produces a dynamic method that is listed outside the extent of the class. And this is feasible with __call() and __callStatic() magic methods. These are used based on the object reference and the static reference individually.

__call() : This is used for calling overloaded methods in the object reference.

__callStatic(): This is used for calling overloaded methods in static reference.

The below example will help you to learn more about method overloading.

<?php 
class SOFT {		
	public function __call($fname, $aurg) { 
		echo "This is a Calling object method '$fname' "
			. implode(', ', $aurg). "\n"; 
	} 
	public static function __callStatic($fname, $aurg) { 
		echo "This is a Calling static method '$fname' "
			. implode(', ', $aurg). "\n"; 
	} } 
// Create new object 
$obj = new SOFT; 

$obj->testRun('This is in object context', "<br>"); 

SOFT::testRun(' This is in static context'); 

?> 
Output
This is a Calling object method ‘testRun’ This is in object context,
This is a Calling static method ‘testRun’ This is in static context

Property Overloading

Like method overloading, Property overloading performs dynamic properties when the object is placed. This property is connected with the class instance, and if this property is not listed within the size of the class, it is estimated as overloaded property. For this, you require not to write any extra code. The following methods tell us more about the same.

Before making the operations, we should describe appropriate magic methods. which are,

  1. __set(): This method triggered while initializing overloaded properties.
  2. __get(): It will be triggered while using overloaded properties with PHP print statements.
  3. __isset(): Method is invoked when we check overloaded properties with isset() function
  4. __unset(): Similarly, this function will be invoked on using PHP unset() for overloaded properties.
<?php
class SOFT{
private $str;
public function __set($name,$value){
$this->str[$name] = $value;
}
public function __get($name){
echo "Property Overloaded name = " . $this->str[$name] . "<br/>";
}
public function __isset($name){
if(isset($this->str[$name])){
echo "The Property name is set.<br/>";		
} else {
echo "The Property name is not set.<br/>";
}}
public function __unset($name){
unset($this->str[$name]);
echo "\$$name is unset <br/>";
}}
$objSOFT = new SOFT;
/* setters and getters on dynamic properties */
$objSOFT->overloaded_property = "new";
echo $objSOFT->overloaded_property . "\n\n";
/*Operations with dynamic properties values*/
isset($objSOFT->overloaded_property);
unset($objSOFT->overloaded_property);
isset($objSOFT->overloaded_property);
?>
Output
Property Overloaded name = new
Property ame is set.
$overloaded_property is unset.
Property ame is not set.

1 thought on “Function Overloading in PHP”

Leave a Comment

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

Scroll to Top