Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3 points in PHP function parameters

Tags:

php

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

like image 558
prgrm Avatar asked Mar 06 '17 08:03

prgrm


3 Answers

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.

like image 129
Bradley Avatar answered Oct 09 '22 16:10

Bradley


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

like image 3
Sivagopal Manpragada Avatar answered Oct 09 '22 14:10

Sivagopal Manpragada



function foo(...$params)
{

  print_r($params);

}

foo('a','b');

Output

array('a','b');

like image 1
dılo sürücü Avatar answered Oct 09 '22 15:10

dılo sürücü