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.
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.
Well, in PHP we have some magic methods which we can use to call these undeclared 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).
__call()
magic methodWe 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.
__call()
magic methodpublic 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.
__callStatic()
magic methodThis 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.
__callStatic()
magic methodpublic 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 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.
__set()
magic methodThis 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.
__set()
magic methodpublic function __set($property) {}
__get()
magic methodThis 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.
__get()
magic methodpublic function __get($property) {}
<?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.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…