Yesterday, one of my friend, coworker wanted to know about how to overload methods in PHP. So, I decided to write something about this in my blog, because there may be many programmers who faced this problem.
Method overloading is common in many object-oriented languages but is not as use-ful in PHP because you tend to use flexible types and the (easy-to-implement) optional function parameters instead. Therefore the method overloading in PHP is implemented some what differently than other programing languages such as Java.
In PHP there is a special class method for implemenging methods overloading. In PHP there are a number of class methods with special meanings whose names begin with a double underscore (__), such as __get(), __set(), __construct(), and __destruct(). Here, we use the method __call(), which is used in PHP to implement method overloading.
To use it, you implement a __call() method, as given below:
The __call() method should take two parameters.The first contains the name of the method being invoked, and the second contains an array of the parameters passed to that method.You can then decide for yourself which underlying method to call. In this case, if an object is passed to method display(), you call the underlying displayObject() method; if an array is passed, you call displayArray(); and if something else is passed, you call displayScalar().
To invoke this code, you would first instantiate the class containing this __call() method (name it overload) and then invoke the display() method, as in this example:
The first call to display() invokes displayArray(), and the second invokes displayScalar().
Referenceses:
Method overloading is common in many object-oriented languages but is not as use-ful in PHP because you tend to use flexible types and the (easy-to-implement) optional function parameters instead. Therefore the method overloading in PHP is implemented some what differently than other programing languages such as Java.
In PHP there is a special class method for implemenging methods overloading. In PHP there are a number of class methods with special meanings whose names begin with a double underscore (__), such as __get(), __set(), __construct(), and __destruct(). Here, we use the method __call(), which is used in PHP to implement method overloading.
To use it, you implement a __call() method, as given below:
public function __call($method, $param){
if ($method == ‘display’) {
if (is_object($param[0]))
$this->displayObject($param[0]);
else if (is_array($param[0]))
$this->displayArray($param[0]);
else
$this->displayScalar($param[0]) ;
}
}
The __call() method should take two parameters.The first contains the name of the method being invoked, and the second contains an array of the parameters passed to that method.You can then decide for yourself which underlying method to call. In this case, if an object is passed to method display(), you call the underlying displayObject() method; if an array is passed, you call displayArray(); and if something else is passed, you call displayScalar().
To invoke this code, you would first instantiate the class containing this __call() method (name it overload) and then invoke the display() method, as in this example:
$ov = new overload;
$ov->display(array(1, 2, 3));
$ov->display(‘cat’);
The first call to display() invokes displayArray(), and the second invokes displayScalar().
Referenceses:
- PHP and MySQL Web Development, Luke Welling and Laura Thomson, Third Edition.
- Official PHP documentation – http://php.net
Comments
Thanks for the information
This is the single most important thing holding PHP back as a proper OO language and it is more than about time it was addressed.
Kind regards
Greg
In PHP there are no restrictions for bad practices, it is a self disciplinary practice of developers to follow good practices. Therefore, PHP can be used for both small scale and enterprise level projects.
Actually, according to my point of view, there no bad languages, but some bad developers are there.
Thanks for the comment...
Regards