As software engineers, we sometimes have to perform certain tasks that can be achieved with a variable number of inputs.
To solve this problem, you can create multiple functions to solve for the different possible number of inputs. Or we can write a large function to solve this problem.
A better way to do this is to create different variations of a simple function to solve for the different scenarios you have.
In this tutorial you will learn how PHP lets you easily solve this problem using method overloading and how you can use these variations.
Polymorphism is a Greek word that literally means many forms. In programming terms, it is defined as the ability of objects of different classes to respond differently based on the same message.
Polymorphism is the concept that lets classes share the same interface and have different definitions for the same method. Polymorphism is one of the key concepts in Object Oriented Programming (OOP).
Method overloading is a concept that allows you to have a method that can perform differently based on its number of parameters. It allows you have multiple definitions for a same method in the same class.
This method will have the same name for all its uses, but might produce different output in different situations. Method overloading is a key concept under the umbrella of polymorphism.
For example, say you have anadd
method that you want to use to sum a couple of numbers in these ways:
function add(int $a, int $b): int
{
return $a + $b;
}
function add(int $a, int $b, int $c): int
{
$sum = $a + $b + $c;
return $sum > 10 ? 10 : $sum;
}
In the first definition, the method takes in two parameters and simply returns their sum. In the second definition, it takes three parameters and it returns the sum of these only when it's equal to 10 or less.
Now, unlike other programming languages, PHP doesn't really let you redefine a method multiple times like this:
class SampleClass
{
function add(int $a, int $b): int
{
return $a + $b;
}
function add(int $a, int $b, int $c): int
{
return $a + $b + $c > 10 ?? 10;
}
}
You would get an error like this:PHP Fatal error: Cannot redeclare SampleClass::add()
. But PHP supports method overloading using a magic keyword,__call
.
__call
keywordThis is a magic method that PHP calls when it tries to execute a method of a class and it doesn't find it. This magic keyword takes in two arguments: a function name and other arguments to be passed into the function. Its definition looks like this:
function __call(string $function_name, array $arguments) {}
Using this magic method, you can create as many methods and as many variations of each of these methods as you like.
For example, to achieve our intended goal with theadd
function, update the__call
definition and yourSampleClass
to be like this:
class SampleClass
{
function __call($function_name, $arguments)
{
$count = count($arguments);
// Check function name
if ($function_name == 'add') {
if ($count == 2) {
return array_sum($arguments);
} else if ($count == 3) {
return array_sum($arguments) > 10 ? 10 : array_sum($arguments);
}
}
}
}
The code is pretty self explanatory. Here's a step by step breakdown:
count
method to know how many arguments are passed to your method.__call
will house all the different methods you intend to create variations of, so the name should be unique and be used to group variations of the methods.add
method, PHP checks for a method with the same name in the class, if it doesn't find it, it calls the__call
method instead, and that is how the code is run.To call theadd
method now, create a new instance of theSampleClass
class and try it out.
$sampleObject = new SampleClass;
echo $sampleObject->add(12, 12) . PHP_EOL; // Outputs 24
echo $sampleObject->add(12, 2, 6) . PHP_EOL; // Outputs 10
The two variations work perfectly ????.
Hope you now understand what method overloading is in PHP, and how you can use it to write better PHP applications.