PHP 5.6 and the Splat Operator
Variadic Functions
This feature allows you to capture a variable number of arguments to a function, combined with “normal” arguments passed in if you like. It’s easiest to see with an example:
function concatenate($transform, ...$strings) { $string = ''; foreach($strings as $piece) { $string .= $piece; } return($transform($string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");
The parameters list in the function declaration has the ...
operator in it, and it basically means ” … and everything else should go into $strings
“. You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings
array, ready to be used.
Argument Unpacking
This has a less sexy name than variadic functions, and it uses the same operator, but this is the one that made me describe PHP code as “wild” – not something that happens often! I like it because it gives a different way of using functions that already exist so it becomes relevant as soon as you upgrade to 5.6.
Variadic functions allow you to declare an array of incoming parameters, and argument unpacking allows you to pass an array in to a function expecting separate parameters in the traditional way; they are absolutely complementary to one another. To use this feature, just warn PHP that it needs to unpack the array into variables using the ...
operator. A simple example could look like this:
$email[] = "Hi there"; $email[] = "Thanks for registering, hope you like it"; mail("[email protected]", ...$email);
You can pass all of your arguments in as an array (pro tip: associative arrays won’t work here), or just the last however many you like, and PHP will take your array and pass each element in as the next parameter in turn. I think this is pretty neat :)
PHP 5.6 Is Coming
PHP 5.6 isn’t released yet, it’ll be out probably sometime this summer. While it’s feature list isn’t huge, that means the upgrade pain shouldn’t be huge either – and neat additions like the two features I’ve picked out here are very welcome additions in my book. PHP gets better with every release and I tip my hat to all those involved – thank you all!
Cool post. Thanks for taking time in writing it!
I wasn’t aware of this new features.
It should require some time before we can be able to use this new features in libraries (for the sake of backward compatibility) but in our actual application code base it could be useful to have something like variadic functions and argument unpacking.
It will help us to avoid calls to `func_get_args` and keep our code shorter and cleaner. Well probably it’s not the most common need, but it always good to have new ways to solve particular problems.
It would be cool if associative arrays could be used to pass named parameters to a function, i.e.
function foo($bar, $baz=null) {
…
}
$args = [‘baz’=>’String’, ‘bar’=>123];
foo(…$args); // equivalent to foo(123, ‘String’)
Although it’s unlikely I’ll be using argument unpacking, because it seems too “magic”. Perhaps I’ll use it as an alternative to call_user_func_array() in some cases.
In most other languages where the feature of ‘accepting a number of arguments and dropping them into an array’ is known as a “rest parameter”. This can be useful if you do searches :)
It’s nice that you don’t have to use a func_get_args() call, shortens the code a bit. Being able to tell people reading your function that it accepts endless arguments is definitely a plus.
But what I’m really excited about is the possible to pass along endless references arguments (https://wiki.php.net/rfc/variadics#by-reference_capture). I’ve been searching for this feature, more than a couple of times, now I don’t have to search anymore (now I just have to wait for PHP on our servers to be updated).
Pingback: PHP 5.6 and the Splat Operator | Advanced PHP |...
Pingback: http www lornajane net posts 2014 php 5-6… | Ideato's hideout
Pingback: Episode 8: Do you even polyglot? | PHP Podcasts
Pingback: PHP5.6, Splat Operators and Argument Un-Packing | Tony Collings' Technical Blog
Pingback: ZendInput and empty values | Rob Allen
Pingback: Method overloading is possible in PHP (sort of) - murze.be
Pingback: Freek Van der Herten: Method overloading is possible in PHP (sort of) – SourceCode
I must be missing something here. Isn’t it passing an array as an argument and then treating it as an array in the method or function? Couldn’t we already do this without that operator? I am clearly totally missing the point of this operator.
The point is, that you can declare a function like this:
[code]
function foo(int …$params){
//code
}
[/code]
and then call it like this:
[code]
foo(3, 7, 4, 8, 0);
[/code]
One one I look at it is, with the operator, you can receive an unknown number of arguments in your function, whereas if you expect an array of parameters, your function only accepts one parameter. Accepting a variable number of variables has value, especially if you are overloading your function.
Pingback: Kenny
I know I’m way late to the game, but thank you for posting this Lorna. I just wrote a blog post about it and gave you credit.
Pingback: Tarik