Tony Marston's Blog About software development, PHP and OOP

The case against function overloading in PHP

Posted on 18th July 2023 by Tony Marston
Introduction
In statically typed languages
Function Overloading in C++
Method Overloading in Java
In dynamically typed languages
Function Overloading in PHP
Adding "proper" overloading to PHP
References
Comments

Introduction

What do I mean by "overloading"? In wikipedia it is described as:

In some programming languages, function overloading or method overloading is the ability to create multiple functions of the same name with different implementations

Here the term "implementation" is wrong as it is the number and type of each argument which should be different, but the implementation should be the same. If the implementation were to be different this would be confusing to anybody reading the code because it is always assumed that a function with a particular name does exactly the same thing but with different argument types. If the implementation were to be different then the reader would have to find the version of that function with a particular set of argument types in order to discover what it actually did.


In statically typed languages

Function overloading was the solution to a problem which exists in all statically typed languages, but as PHP is dynamically typed it does not have the same problem therefore it does not need the same solution

In statically typed languages when you define a variable you must also define its type (int, double, boolean, string, etc). It then becomes impossible to assign a value to that variable which has a different type. When you define a function with arguments you must also define the type for each of those arguments so that when you call that function with a list of arguments the type of each argument on the call must match the type for that argument in the function's definition. It is not possible to supply an argument of type int to a function that expects a double. This form of type checking is performed at compile time.

Function Overloading in C++

In Function Overloading in C++ it is described as:

Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading "Function" name should be the same and the arguments should be different.

Here are some examples:

int add(int a, int b)  // two int parameters
{
  return (a + b);
}
 
double add(double a, double b)  // two float parameters
{
  return (a + b);
}

int add(int a, int b, int c)  // three int parameters
{
  return (a + b + c);
}

Method Overloading in Java

In Method Overloading in Java it is described as:

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading.

Here are some examples:

public int sum(int a, int b) {  // two int parameters
  return (a + b);
}

public double sum(double a, double b) {  // two float parameters
  return (a + b);
}

public int sum(int a, int b, int c) {  // three int parameters
  return (a + b + c);
}

In dynamically typed languages

In dynamically typed languages such as PHP which are not compiled any type checking can only be performed at run time. Unless the declare(strict_types=1); directive is used (refer to Strict Typing) it is not necessary for an argument in a function call to have the same type as that argument in the function's definition. This is because of a feature called Type Juggling which will attempt to automatically convert each argument to the expected type within the called function.

OO purists like to say
everything is an object,
but when writing software which deals with HTML at the front end and SQL at the back end the simple fact is that
everything is a string

This is because PHP was designed from the outset to build applications using HTML documents at the front end and a SQL database at the back end, and both of these technologies are typeless.

As you should see all the values in the HTML front end and SQL back end do not have any type other than "string", so converting values to particular types in the middle layer would seem to be a pointless exercise as they always have to be converted back to strings.

Until Type Hinting was gradually added as an option to PHP starting with version 5 it was not possible to declare a variable's type either in your code or in any function definition which included both internal and user-defined functions. It was therefore possible to pass a variable of any type to any function and for that function to automatically perform any type juggling if at all possible. If necessary the developer could insert code at the start of each user-defined function to examine each argument's type and either coerce it manually or reject it by throwing an exception.

This means that it is possible in PHP to declare a function/method as follows:

function add ($arg1, $arg2)
{
  if (!is_numeric($arg1) {
    throw new Exception ("Arg1 is not numeric");
  }
  if (!is_numeric($arg2) {
    throw new Exception ("Arg2 is not numeric");
  }
  $result = $arg1+$arg2;
  return $result;
} 

In this example it does not matter if either of the input arguments is an integer, a float or a string as it will work regardless. This function can accept arguments in a mixture of types WITHOUT having to declare multiple versions of the same function with different hard-coded types. Note here that strings such as "9" and "9.99" can be automatically converted to the equivalent integer or double type without any issues, but that strings such as "nine" will be rejected. Note also that while the values are converted for use within the function they are not converted for the calling code.

Method Overloading in PHP

While there is a section in the PHP manual called Overloading this does *NOT* operate in the same way as in other OO languages like C++ and Java. Instead it relies on Magic Methods such as the following:

Note that these do *NOT* allow you to call the same function/method with different parameter types as shown in the examples for languages which are statically typed. In PHP once a function has been declared it is simply not possible to declare it again otherwise you will get a fatal error with the message "Cannot redeclare xxx() (previously declared in ...)".


Adding "proper" overloading to PHP

Ever since type hinting was added to PHP in an attempt to make it behave more like a statically typed language there have been calls from a growing number of developers to add "proper" function overloading to the language so that they can use code as shown in the examples for C++ and Java. They simply do not understand that function overloading was only added in order to provide a solution to a problem with strict typing whereby it is physically impossible to change a variable's type so that it can be used as an argument for a function that expects a different type. This limitation does not exist in a dynamically typed language as all type checking is performed at run time with the ability to automatically perform type juggling, as shown in this example.

Programmers who don't like using a dynamically-typed language should not be using PHP in the first place

Those programmers who prefer to use a statically typed language simply because they are unable to wrap their heads around how the more modern dynamically typed languages work should stop their whingeing and whining and switch to another language which does not offend their delicate sensibilities or take them out of their comfort zone. They should remember that a programming language is just a tool, and if they don't like the tool they are currently using then they should do the intelligent thing and switch to a different one. After all, their are literally hundreds to choose from, so they should be able to find at least one that they like.

PHP was designed from the outset to be dynamically typed. This has worked so well over the past 20+ years that it has become the most popular language in the entire world for building web applications. It has been generally recognised that programmers using a dynamically typed language find it easier to use and can be more productive than when using the older statically typed languages. Any attempts to change PHP's DNA to make it acceptable to programming neanderthals should be regarded as a backwards step and should be resisted.

It would appear that the core developers have come to their senses and have finally agreed that function overloading has no place in PHP, as witnessed in PHP RFC: Deprecate functions with overloaded signatures.


References

These are reasons why I consider some ideas on how to do OOP "properly" to be complete rubbish:


counter