26

I was working with Laravel 5.3 and in one of the functions, I found this piece of code:

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

    return $next($request);
}

The code comes from \Illuminate\Auth\Middleware\Authenticate::class.

What are those 3 dots before $guards variable?

Salar
  • 5,305
  • 7
  • 50
  • 78
  • 1
    https://wiki.php.net/rfc/argument_unpacking - if you're ever unsure, there's a comprehensive list of symbols in http://stackoverflow.com/questions/3737139/reference-what-do-various-symbols-mean-in-php?rq=1 – Qirel Nov 05 '16 at 15:10

1 Answers1

32

It indicates that there may be a variable number of arguments.

When the function is called with more than 3 arguments, all the arguments after $next will be added to the $guards array.

You can read about it here.

Script47
  • 14,230
  • 4
  • 45
  • 66
Daniel Enache
  • 391
  • 3
  • 5
  • 2
    Will `$guards` still be an array if a ONLY 3 parameters are sent? – Script47 Mar 07 '18 at 15:25
  • Just noticed the last time the answerer was available (Dec 11 '16 at 19:52), if anyone else stumbling across can answer my above comment, please do so. – Script47 Mar 07 '18 at 15:28
  • 2
    Confirmed, `$guards` will still be an array, please click [here](https://repl.it/repls/LightgreenThornyWorkspaces) to see an example. – Script47 Mar 07 '18 at 16:47