Function overloading in PHP

Why it is and isn’t supported.


Although PHP doesn’t handle function overloading in quite the same way as some strongly typed programming languages, it does support the ability to overload functions. Just in a different way.

Because PHP uses type inference to deduce the type of values your variables contain, it isn’t able to differentiate between two functions of the same name with different parameters.

For example, if we wanted to create an add() function that could add two numbers, or concatenate two strings, in some languages we could write something like this;

The first thing you may notice if you use an IDE is that it reports an error. In NetBeans, we get the following error:

Method or function “add” has already been declared

PHP doesn’t allow you to declare two functions in the same namespace with the same name. This is because If you were to invoke the function, it wouldn’t know which of the two functions you meant to call. Although PHP supports explicit type casting, it does not support declaring a variable of a particular type, so it doesn’t support “classic” function overloading. The manual refers to this as Type Juggling.


Solution: One function, and a little extra logic.


Okay, maybe more than just a little.

PHP has several built-in functions which allow you to access the parameters of a function. This is what gives you the ability to overload your functions. Although the syntax is somewhat more verbose, it is possible to achieve function overloading in much the same way as the example above. However, instead of creating multiple functions with the same name, you create one function, and call different code blocks based on the number and the type of parameters. The main difference here is that you have to do it in a very explicit way.

When function_num_args() is called inside a function, it will return the number of parameters that were passed to the function during that function call. Using this, we can specify which section of code we want to run, based on the number of parameters passed into the function when it is called.

func_get_arg() allows us to access the values passed to the called function. The function takes a zero-based index value, so the first parameter is at position 0, the second at position 1, and so on.

Finally, we can call the is_string() and is_int() functions on the returned values. These functions will return either true or false, based on whether the values passed to them are integers or strings, respectively. There are several of these functions available in PHP. For example, if we wanted to handle floating point numbers as well, we could use the is_float() function. Using additional logic, we could call the same block of code whether a number is a floating point or an integer value, or handle these types of number entirely differently.

Now when we call the function;



Should you use it?


This is entirely up to you. It could be argued that function overloading in this manner violates the Separation of concerns principle (although it is a principle, not a hard-and-fast rule). Provided the code is readable and easy to maintain, and makes sense for its purpose, then used wisely, it can add additional flexibility to your code when handling various data-types. You might, for example, want to round floating point numbers to a certain level of precision, or ensure integer values are always returned to two decimal places, such as when dealing with currency values.

Leave a Reply

Your email address will not be published. Required fields are marked *