Splat!

While looking through the Query Builder class in the Laravel source code today, I came across something I hadn’t seen before in PHP:

$query->{$method}(...array_values($value));

I had no idea what the three dots meant, so I went looking for it. Turns out it’s a feature added in PHP 5.6 called “variadic functions”, and is often referred to as the “splat operator”, or “scatter operator”. The page I found was from Lorna Jane, and explained it well. Basically, it lets you pack as many parameters as you want to pass in to a function into a single array. Here’s an example:

If you remove the three dots, you get this:

But wait! There’s more!

Turns out this triple dot thing can work both ways. It can also turn an array into individual parameters, like this:

This usage is called Argument Unpacking. Here’s what happens if you don’t put the three dots:

This shows that it is in fact unpacking that array into the individual input variables for the test function.

Thanks Lorna Jane for the great explanation!