The spread operator (...
) transforms an array (or generically a Traversable
) into an argument list, and vice-versa. If you're looking for anything more fancy than this, you're out of luck: the Javascript implementation is simply more powerful, in that it also transforms object expressions into key-value pairs, and vice-versa.
How might you use this? Well, consult the original RFC that defined it for some practical examples:
- More performant than
call_user_func_array
- Extending variadic functions: forwarding
- Partial application: multiple unpacks
I personally use it because of that first point. As mentioned in the RFC:
The ... argument unpacking syntax is about 3.5 to 4 times faster than call_user_func_args. This solves the performance issue. Benchmark code and results.
You might also ask why PHP didn't implement the object unpacking, like Javascript. That too is answered in the RFC:
In order to ensure forward-compatibility with named arguments the unpacking operator does not support string keys. If a string key is encountered during unpacking a recoverable error is thrown. If the error is ignored using a custom error handler, no further arguments will be unpacked but the call still happens.
I personally find this to be a trap, which is why I abstained from voting on this RFC.