//_Semicolon;

Will code for food; The ramblings of Talifhani Luvhengo;

PHP Function/Method Overloading

“Function overloading or method overloading is a feature found in various programming languages such as Ada, C#, VB.NET, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function.” — Wikipedia(http://en.wikipedia.org/wiki/Function_overloading)

I’m sure by now you have realised that PHP does not natively support function overloading. It can get annoying, especially if like me you are from a Java(Or whatever Full OO language you’re from) background. I doubt its coming to PHP anytime soon, haven’t really looked at the road map but from first glance it would be too big a change and render function like __call(), __get(), __set() a bit useless. So what is a developer with too much time in his hands to do? Well come up with a way to simulate this behavior of cause ๐Ÿ™‚

Lets have a quick look at a scenario:

So you write a function to add two numbers and return the sum. Lets call it sum():

function sum($a, $b){
return $a + $b;
}
echo sum(3,5); //8 ๐Ÿ™‚ works

And then you decide you want a function to “add”(concatenate) two strings, lets call it concatenate():

function concatenate($a, $b){
return $a . $b;
}
echo concatenate("Hello ", "World"); //Hello World

So now we have out two awesome functions that do exactly what we expect them to do. But now you figure you should just have one function, lets call it add() to do those two tasks depending on the parameters submitted. That’s where the problems begin with PHP. In Java for example you could just define two function with same name but that take different parameters. e.g: int function add(int a, int b) and another string function add(string a, string b). In PHP we need to do some heavy lifting ourselves:

So the solution lies in the native functions func_get_args() and func_num_args(). PHP Lets you declare functions that take an arbitrary number of arguments/parameters and useย func_get_args() andย func_num_args() to retrieve and count the arguments respectively.

The solution:

function add(){
  //Make sure that exactly two parameters are sumitted
  if(func_num_args() == 2){
    $args = func_get_args();
    $a = $args[0];
    $b = $args[1];
  }else{
    trigger_error("Incorrect parameter count for function " . __FUNCTION__ . "()", E_USER_ERROR);
  }
  /** Check if two integers where submitted **/
  if(is_int($a) && is_int($b)){
    return sum($a, $b);
  }elseif(is_string($a) && is_string($b)){
    /** Check if two strigns where submitted **/
    return concatenate($a, $b);
  }else{
    /** Otherwise give an error **/
    trigger_error("Incorrect parameter type for " . __FUNCTION__ . "()", E_USER_ERROR);
  }
}
echo add("Hello ", "World"); //Hello World
echo add(5, 4); //9
echo add("Hello", 3);//Error
echo add(3, "World");//Error
echo add("HELLO", NULL);//Error

I don’t think i’d ever use it really. Seems like a lot of work for something that you could do other quicker ways. But use it, don’t use it ๐Ÿ™‚
The function above is commented so just follow the comments if you don’t understand anything.

Get Adobe Flash player

Page optimized by WP Minify WordPress Plugin