How to perform method overload with PHP?

Asked

Viewed 5,497 times

12

How to perform method overload with PHP?

To declare methods with the same name but with different parameters.

  • 3

    No method overload in PHP, only overwritten.

  • 3

    PHP does not overload. It has only the little ways to simulate

2 answers

15


PHP is a dynamic language, so this makes no sense.

There’s even a concept called overloading but that is a little different from what you are thinking, but gets similar result.

In dynamic languages the parameters can receive any type so the resolution of what to do with them should be given at runtime through selection (if, switch, element of 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 the most common is that a method does more than one thing based on the parameter. Yeah, I know, for those who are used to everything organized in single functions seems strange but often gets interesting and saves code.

Usually it is said that a parameter is of the type Mixed. This type does not actually exist, it is only an indicative that can be used more than one type of data there. Example:

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

I put in the Github for future reference.

The function shows that it is possible to send parameters with various types and the function return may also vary. It may seem like a mess but simplifies the use of language 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.

6

As stated before, there is no method overload in PHP.

What can be done is to use function arguments in dynamic ways, as I’ve seen in some frameworks such as 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, although they are not previously stated in the function creation (in my example, there are no arguments in the method declaration MyClass::value).

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

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

Refactored example 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;
       }

    }
}

In my view, the difficulty of implementing this would be to document the code of its method or function.

Test these codes on Phpsandbox

Browser other questions tagged

You are not signed in. Login or sign up in order to post.