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?