How to perform methods overload with PHP?

10

How to perform overloading of methods with PHP?

In order to declare methods with the same name but with different parameters.

    
asked by anonymous 12.02.2015 / 18:05

2 answers

12

PHP is a dynamic language, so it does not make sense.

There's even a concept called overloading but that's one little different from what you're thinking but you get similar results.

In dynamic languages the parameters can receive any type so the resolution of what to do with them must be given at runtime by selection ( if , switch , element array or otherwise).

If you really want to have methods that do almost the same thing with different parameters you have to change the name. But most commonly a method does more than one thing based on the parameter. It's, I know, for those who are accustomed to everything organized into unique functions it seems strange but it often gets interesting and saves code.

Usually it is said that a parameter is of type mixed . This type does not really exist, it's just an indication that you can use more than one type of data there. Example:

mixed str_replace(mixed $search, mixed $replace, mixed $subject[, int &$count])

The function shows that it is possible to send parameters with varied types and the return of the function can also vary. It may seem like a mess but it simplifies the use of language by allowing a function to accomplish the task in several different ways. Of course, errors can only be checked at runtime.

Each with its advantages and disadvantages.

    
12.02.2015 / 18:15
4

As mentioned earlier, there is no overloading of methods in PHP.

What can be done is to use function arguments in dynamic ways, as I've seen in some frameworks like CakePHP and Laravel 4.

Example:

<?php


class MyClass{

    protected $values = array();

    function value()
    {
       $count = func_num_args();

       // obtém dados
       if ($count == 1) {

            $key = func_get_arg(0);

            return $this->values[$key];
       }

       // define dados
       if ($count == 2) {

            list($key, $value) = func_get_args();

            $this->values[$key] = $value;

            return $this;
       }

    }
}


$class = new MyClass;

$class->value('key', 'stackoverflow português');

echo $class->value('key'); // Stackoverflow português

print_r($class);

That is, through the functions func_get_arg , func_get_args and func_num_args , you can access the arguments passed in the functions, even though they are not previously declared in the function creation (in my example there are no arguments in the MyClass::value method declaration).

In PHP 5.6, we have a nice feature in PHP called Variadic Function .

Instead of using the three functions that take the arguments mentioned, we only use the ... operator.

Example refactored for PHP 5.6:

class MyClass{

    protected $values = array();

    function value(...$args)
    {
       $count = count($args);

       // obtém dados
       if ($count == 1) {

            $key = $args[0];

            return $this->values[$key];
       }

       // define dados
       if ($count == 2) {

            list($key, $value) = $args;

            $this->values[$key] = $value;

            return $this;
       }

    }
}

As I see it, the difficulty in implementing this would be to document the code of your method or function.

Test these codes on PHPSandBox

    
12.02.2015 / 20:20