What is the name of the ... operator used in PHP 5.6?

Asked

Viewed 204 times

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.

  • @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).

  • 3

    Yes. I gave that name now.

  • You couldn’t do that with func_get_args? PHP, simply PHP

  • As @rray said, PHP is switching funções for operadores. And it gives a bigger job using func_get_args. In the question what will change with variadic Function explains the gains.

  • 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).

Show 1 more comment

2 answers

11


Splat (or, Scatter, or Spread).

It is an operator to indicate that its function can have any number of parameters. This name is specific to PHP. In other languages, the name changes.

If your function has splat in the statement and has more than one argument, the splat must necessarily be the last argument.

  • Interassante. I think in the python an asterisk would be used. *args.

  • Yes, one or two asterisks. It has both versions.

  • If you read the PHP manual in English you can see that this is wrong: "Arrays and Traversable Objects can be unpacked into argument lists when Calling functions by using the ... Operator. This is also known as the splat Operator in other Languages, including Ruby. "splat is the ruby name.

  • Two asterisks would be for kwargs. In the case of PHP (compared to python), would be the *args. Because it’s a list. And in PHP ... only works in arrays numerically indexed.

  • 1

    I’m not talking about python Just to please :D

  • @There’s nothing wrong with Blunt. The term is well accepted by employees. There’s even one more, the "spread".

  • @Ciganomorrisonmendez had never really heard that term, only variadic. but wrong is too strong, you’re right.

  • I think he’s called ellipses also, @Ciganomorrisonmendez.

  • This one I didn’t know.

Show 4 more comments

3

  • +1 . Well, I think I’m learning a lot about the name of the operators here at SOPT :)

Browser other questions tagged

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