Murray Picton

Overloading PHP functions

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):

<?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:

<?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:

<?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