3
PHP 5.6 implemented a feature called Variadic Function.
It is as if they were infinite arguments. They can be used both in the statement of a function as for the calling.
Examples PHP 5.6
Example in the statement:
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 used in versions prior to 5.6
, could be done as follows:
Statement example:
function test($arg)
{
$args = array_slice(func_get_args(), 1);
print_r($args);
}
test('first argument', 1, 2, 3); // array(1, 2, 3)
Example calling:
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 anyone using PHP 5.6, which will be the purpose of the functions call_user_func_array
and func_get_args
?
That implementation of variadic function
could compromise these functions and, in the future, render them obsolete?
I agree they are advantages. But this answers what you were asked (by yourself)?
– bfavaretto
@bfavaretto, it was just an additional detail that I think could have been put into some of the questions.
– Wallace Maxters
Maybe that fits the question itself.
– bfavaretto