What can change with the implementation of variadic function?

3

PHP 5.6 has implemented a feature called Variadic function .

It's like infinite arguments. They can be used both in the declaration of a function and for the call.

Examples PHP 5.6

Example in the declaration:

function test($arg, ...$args)
{
    print_r($args);
}

test('first argument', 1, 2, 3); // imprime: array(1, 2, 3);

Example in the call:

function test($first_name, $last_name)
{
    return "{$first_name} {$last_name}";
}

$args =  ['Wallace', 'Maxters'];

$alias = 'test';

$alias(...$args); // Wallace Maxters

test(...$args); // Wallace Maxters

Examples versions prior to 5.6

These examples if they were used in versions prior to 5.6 , could be done as follows:

Example declaration:

function test($arg)
{
    $args = array_slice(func_get_args(), 1);

    print_r($args);
}

test('first argument', 1, 2, 3); // array(1, 2, 3)

Calling example:

function test($first_name, $last_name)
{
    return "{$first_name} {$last_name}";
}

$args = ['Wallace', 'Maxters'];

$alias = 'test';

echo call_user_func_array($alias, $args); // Wallace Maxters
echo call_user_func_array('test', $args); // Wallace Maxters

After the implementation of variadic function , for those who use PHP 5.6, what will be the purpose of the functions call_user_func_array and func_get_args ?

Can this implementation of variadic function compromise these functions and make them obsolete in the future?

    
asked by anonymous 27.02.2015 / 14:14

3 answers

3

I do not follow closely the developments of PHP, but the little I see gives the impression that they are quite conservative, and that even when something is flagged as deprecated ( deprecated ) a good time to actually be removed. I agree with @gmsantos at this point, PHP avoids as much as possible to create incompatibilities with legacy codes.

As for the two functions mentioned, func_get_args I believe it is kept because it has a function different from the splat parameter of the variable functions, since it returns all arguments received. The call_user_func_array also has its role, because it allows the passage of dynamic arguments even if the function is not variable. This is without saying that it still allows calling a function by name (as string), such as call_user_func . In summary, I see reasons for the two functions to be maintained, mostly func_get_args .

    
27.02.2015 / 15:36
1

In my view the variadic function will replace call_user_func_array and func_get_args .

They would stick around for a few releases to maintain code compatibility that would need to work in earlier versions of PHP 5.6 until one fine day go to vote to be discontinued .

Do not expect this to happen soon. The latest versions included alternate syntax for other things, such as the array syntax in PHP 5.4 , which replaces the array() function and the Potentiating Operator also in PHP 5.6 which does the same thing as the pow() . Both cases remain valid until it is decided that it is no longer worth keeping them there, as has happened various things (example of the extension mysql_* ).

    
27.02.2015 / 15:07
0

Just to complement the previous answers: a simple example is improving syntax and decreasing the call of functions, improving performance.

Formerly:

call_user_function_array(array($controller, $method), $parameters);

Currently:

$controller->$method(...$parameters);

Another advantage can be seen in the declaration of methods or functions. I will give an example that I always use, which is the use of a static method to facilitate the creation of the class instance.

Formerly:

 class Controller {

    public function __construct(Request $request, Response $response)
    {

    }

    public static function make(Request $request, Response $response)
    {
         return new static($request, $respone);
    }

}

Currently:

class Controller {

    public function __construct(Request $request, Response $response)
    {

    }
    /** Não precisa declarar as mesmas coisas de construct, é um atalho **/
    public static function make(...$arguments)
    {
         return new static(...$arguments);
    }

}


Controller::make(new Request, new Response);
    
16.09.2015 / 18:44