5

I am looking at Laravel code and found this in the Authenticate.php middleware:

  public function handle($request, Closure $next, ...$guards)
    {
        $this->authenticate($guards);

        return $next($request);
    }

I have never seen such a thing, what do the 3 points do? I have googled it but found nothing

prgrm
  • 3,734
  • 14
  • 40
  • 80

3 Answers3

7

That's a spread operator... here's the relevant documentation: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list

It essentially converts an array into a set of arguments, or converts a set of arguments into an array.

Bradley
  • 2,071
  • 2
  • 21
  • 32
3

PHP has support for variable-length argument lists in user-defined functions. This is implemented using the ... token in PHP 5.6 and later, and using the func_num_args(), func_get_arg(), and func_get_args() functions in PHP 5.5 and earlier. Adding php.net link

http://php.net/manual/ro/functions.arguments.php#functions.variable-arg-list

Sivagopal Manpragada
  • 1,554
  • 13
  • 33
1

function foo(...$params)
{

  print_r($params);

}

foo('a','b');

Output

array('a','b');

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28