Code & the internet

Overloading PHP functions

Post Image

Being able to overload functions is a really useful ability of lots of programming languages including Java, C# and Perl including many others. Unfortunately, in PHP it is not built into the core functionality but it can be created with some thought and playing around with standard PHP features.

So, what is overloading functions?

Overloading a function is the ability to define a function more than once with a different set of parameters for each one and then when it is called, the version of the function that matches the parameter set will be executed.

How to overload a function in PHP

To demonstrate how to overload functions in PHP, let’s build a simple addition function that will allow you to use it 3 different ways: sending 2 integer parameters that will be summed, sending a single array parameter that will return the sum of the whole array, and sending a single string parameter in the format x:y which will sum all numbers from x to y.

Let’s take a look at how it would be done if PHP allowed function overloading (this of course wont work, also type hinting for string and int is not available but I am showing it for clarity):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
function addition(int $x, int $y) { //2 integer values

return $x + $y;
}

function addition(array $arr) { //An array

return array_sum($arr);
}

function addition(string $str) { //A string in the format x:y

$params = explode(":", $str);
$sum = 0;
for($i = $params[0]; $i <= $params[1]; $i++) {
$sum += $i;
}
?>

The way to get around this inability is to use the useful func_get_args() method that allows us to get all of the arguments the have been passed to the function.  Let’s take a look at what we need to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
function addition() {
$amt = func_num_args(); //Get the number of parameters

$args = func_get_args(); //Get the parameters


switch($amt) {
case 1:
if(is_array($args[0])) return additionArray($args[0]);
elseif(is_string($args[0])) return additionString($args[0]);
break;
case 2:
return additionMulti($args[0], $args[1]);
break;
}
}

function additionMulti($x, $y) { //2 integer values

return $x + $y;
}

function additionArray(array $arr) { //An array

return array_sum($arr);
}

function additionString($str) { //A string in the format x:y

$params = explode(":", $str);
$sum = 0;
for($i = $params[0]; $i <= $params[1]; $i++) {
$sum += $i;
}
return $sum;
}

echo addition(1, 2); //3

echo addition(array(1, 2, 3)); //6

echo addition("1:4"); //10

?>

Finally, we can clean up the code a bit and provide more functionality that will allow any number of parameters to be passed in and it will sum them all up and return the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
function addition() {
$amt = func_num_args(); //Get the number of parameters

$args = func_get_args(); //Get the parameters


if($amt == 1) {
if(is_array($args[0])) return additionArray($args[0]);
elseif(is_string($args[0])) return additionString($args[0]);
} else {
return additionArray($args); //Our arguments are an array so we can just sum them all

}
}
function additionArray(array $arr) { //An array

return array_sum($arr);
}

function additionString($str) { //A string in the format x:y

$params = explode(":", $str);
$sum = 0;
for($i = $params[0]; $i <= $params[1]; $i++) {
$sum += $i;
}
return $sum;
}

echo addition(1, 2); //3

echo addition(array(1, 2, 3)); //6

echo addition("1:4"); //10

?>

Thanks for reading!  I hope you have found this useful.  Please take a look around at some of my other posts for more great PHP and programming articles.

Update

Howard over at puremango.co.uk has taken the idea and expanded on it – providing a useful OO solution, go and take a look at http://www.puremango.co.uk/2010/10/overloading-in-php

Related Posts:

One Response to “Overloading PHP functions”

  1. [...] Picton wrote up a blog post today on overloading functions in PHP. Overloading is a useful feature of many languages. Murray gives a nice definition on his post: [...]

Leave a Reply

Previously

Building a PHPDoc parser in PHP

This is the second in a series of posts about how I built Doqumentor – The Runtime PHP Documen


Using the PHP Reflection API to document your code

I have recently spent a bit of time writing a new PHP development tool for documenting code at runti


Exploring the iPhone’s accelerometer through Javascript

The iPhone has had an accelerometer in it since the start, allowing users to tilt the device and it