Skip to main content

Posts

Showing posts from February, 2009

Methods overloading in PHP

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:
public function __call($method, $param){

if ($method == ‘display’) {