-1

I recently learned about PHP's ... token (reference the docs) which is used to support variable-length argument lists. What is the term for this token or how is it pronounced? If it doesn't have a name, is their any similar functionality in another language which has a name?

user1032531
  • 24,767
  • 68
  • 217
  • 387

1 Answers1

2

TL;DR: The tokens name is 'T_ELLIPSIS', used in a function declaration makes this function 'variadic', used when calling a function with an array holding the parameters, it's called 'argument unpacking'.


Its name is T_ELLIPSIS, I found that out using token_get_all and token_name in a psysh-session:

>>> token_get_all('<?php function testit(...$a) { echo $args;}')
=> [
 [
   379,
   "<?php ",
   1,
 ],
....
 [
   391,
   "...",
   1,
 ],
....
>>> token_name(391)
=> "T_ELLIPSIS"
>>> 

Edit: I understood you possibly too literally -- I thought you asked for the token-name, but given the downvote(s), I suspect you meant, how do programmers refer to it in speech and writing (apparently "splat", according to the comments)

Edit 2: Used in a function definition before the last parameter, in other languages a function defined with an ellipsis in PHP, would be called a 'variadic function':

>>> function f(...$a) { return $a; }
>>> f(1, 2, 3, 4)
=> [
     1,
     2,
     3,
     4,
   ]
>>> 

Edit 3: Finally: If you have an array holding values you want to pass to a function, you can use ... to achieve "Argument Unpacking"

>>> function f($a, $b, $c) { return "{$a}-{$b}-{$c}"; }
>>> f(...[1,2,3]);
=> "1-2-3"
>>> 
Community
  • 1
  • 1
Tom Regner
  • 6,856
  • 4
  • 32
  • 47
  • Is that not the name of the constant defined by PHP? I think the OP is asking about the actual term for this sort of operation. – Script47 Jun 07 '18 at 15:04
  • The term "ellipsis" is also not referenced once in http://php.net/manual/en/functions.arguments.php, however, feel it better describes the functionality more than "splat" and will like use "ellipsiser" or something. Then again, splat might be more fun... Maybe mix the two words? – user1032531 Jun 07 '18 at 15:12
  • '*Argument Unpacking*' – Script47 Jun 07 '18 at 15:15
  • Just received a PHP error `PHP Fatal error: Variadic parameter cannot have a default value`. Seems like PHP uses this term as well. I appreciated your post. – user1032531 Jun 07 '18 at 18:09