Function overloading and property overloading in PHP
Hey, Buddy .. I was really confused with function overloading and property overloading in PHP v/s other languages.
But, now I am clear about it, I think so ..
Let’s dive into it.
What is method or function overloading in PHP?
Function overloading means use the same function name with different parameters or return type. It means there must be something which can differentiate them except the name.
But in PHP we can achieve this in a different way. Like if we call the methods which are not declared in the class then it will be method or function overloading.
How can we use the methods which are not declared in the class?
Well, in PHP we have some magic methods which we can use to call these undeclared methods.
Why magic methods?
Because, magic methods are automatically called on the bases of a particular action. Like if we try to call the undeclared or private methods outside the class then it will call the __call()
or __callStatic()
method of the class (if these are declared).
The __call()
magic method
We must define this magic method as public access mode inside the class. It will be automatically called when we try to call the undefined or private method outside the class with the class object.
The syntax of __call()
magic method
public function __call($name, $arguments) { }
The $name
will provide us the name of the method which we are calling and $arguments
will be an array of parameters for that method.
The __callStatic()
magic method
This magic method is used the same as the __call()
method used. But, it is used for only static methods. When we try to call static methods (which don’t exist) outside the class, the __callStatic()
magic method will call automatically.
The syntax of __callStatic()
magic method
public static function __callStatic($name, $arguments) { }
The $name
will provide us the name of the static method which we are calling and $arguments
will be an array of parameters for that method.
Read also : Shopping cart with PHP & MySQL
Property overloading in PHP
Property overloading in PHP means use the properties which are not declared and exist in the class. This will be known as property overloading.
If you are still confused don’t worry, after looking at the example, everything will be clear to you.
To achieve this we can use the magic method __set()
and __get()
. These methods will be called automatically when we try to access private or undefined properties outside the class.
The __set()
magic method
This method will be used to set the value to a property which does not exist in the class. We can declare an array inside the class and then push the key value pair to that array when we try to set value to non-existing or non-accessible properties.
The syntax of __set()
magic method
public function __set($property) {}
The __get()
magic method
This method will be used to retrieve the value of a property which does not exist in the class. We can declare an array inside the class and then push the key value pair to that array with the help of __set()
method and then we can get the value of the particular key of that array with the help of __get()
method.
The syntax of __get()
magic method
public function __get($property) {}
Example of function overloading and property overloading in PHP.
<?php class experiment { private $data = []; public function __set($property, $value) { $this->data[$property] = $value; } public function __get($property) { if(array_key_exists($property, $this->data)) { return $this->data[$property]; } else { return "Property not found!"; } } public static function __callStatic($name, $arguments) { echo "method name = " . $name . "\n"; foreach($arguments as $arg) { echo $arg . "\n"; } } public function __call($name, $arguments) { if($name == "userInfo") { if( count($arguments) == 1 && is_string($arguments[0]) ) { return call_user_func_array([$this, "user_name"], $arguments); } else if( count($arguments) == 2 && is_string($arguments[0]) && is_int($arguments[1])) { return call_user_func_array([$this, "user_name_age"], $arguments); } } } public function user_name($name) { echo $name . "\n"; } public function user_name_age($name, $age) { echo $name ." ". $age . "\n"; } } $obj = new experiment(); // method/function overloading $obj->userInfo("harry"); $obj->userInfo("rohan", 45); // property overloading $obj->name = "Harry"; $obj->skills = "Web development"; echo $obj->name . "\n"; echo $obj->skills . "\n";
Hope this article will be useful for you, please feel free to comment below if you found anything wrong here.