5
From the PHP 5.6
we now have the possibility to invoke or declare a function, stating that the arguments are infinite, through the ...
.
Example:
function add(... $arguments)
{
return array_sum($arguments);
}
add(1, 2, 3); // imprime: 6
Example 2:
print_r(...[$object, false]);
// é equivalente a:
print_r($object, false);
I asked a question related to variadic functions where the advantages of its use are explained.
But I don’t know the name of this operator used in variadic funcion
, the one about the operator ...
.
I usually speak reluctantly. I don’t know if I’m right.
What is the correct name of this operator?
Operator Decompression.
– Diego Souza
@Diegosouza, Descompactator wouldn’t be in the call act? It can also be used in the function declaration (which would make it a compactor, in my view).
– Wallace Maxters
Yes. I gave that name now.
– Diego Souza
You couldn’t do that with
func_get_args
? PHP, simply PHP– Guilherme Lautert
As @rray said, PHP is switching
funções
foroperadores
. And it gives a bigger job usingfunc_get_args
. In the question what will change with variadic Function explains the gains.– Wallace Maxters
An example of this can be by comparing frameworks codes. Instead of using
call_user_func_array(array($controller, $method), $arguments
, could be used like this$controller->$method(...$arguments)
.– Wallace Maxters