

The biggest problem with array packing and unpacking using the splat and spread operators like this is that it would only work with enumerated arrays: using an associative array would throw a fatal error: Sometimes called “array unpacking”, we can also pass an array of values when calling the function, also using the. We can then call the total() function with any number of arguments: variadic (“splat”) operator in front of a function parameter, the function will then accept a variable number of arguments, and the parameter will become an array inside the function. Variadics were introduced in PHP 5.6: when you place the. Instead, I’m going to take a look at the more specific subject of named arguments when used with variadics. And before PHP 8, it was possible to change an argument name without worrying about it, but now such a change becomes a backward-compatible break.Ī lot has already been written on other blogs about named arguments, so I’m not going to regurgitate all that here. With PHP 8, all that has changed, and it is important for library authors to provide a meaningful name for arguments: that’s the drawback for developers of libraries and frameworks that use generic names like $pValue that don’t provide any significance to our users. Until PHP 8, argument names in functions and methods were only an internal implementation for those functions, and had no visibility outside.
Php 8 named arguments code#
We can also provide mandatory arguments as named arguments, again making the code easier to read, because the argument name is visible and obvious in our code, so when we’re reading code that’s been written using named arguments, we’re not dependent on our IDE to tell us what the 3rd argument is, or look it up in the documentation if we can’t remember.Īnd when we’re using named arguments, it doesn’t matter what order we specify them in, so we don’t need to remember whether it’s needle before haystack for string or array searches. It can make our code a lot shorter and easier to read, especially when we’re calling methods with a lot of optional arguments. $now = new DateTimeImmutable(timezone: new DateTimeZone('UTC')) With named arguments, we no longer need to pass all optional arguments to a function or method, only those where we want a value other than the default so if we want to allow PHP to handle the default datetime argument itself, we only need to pass the timezone argument by name: $now = new DateTimeImmutable('now', new DateTimeZone('UTC'))


If you want to create a timestamp for the current time (‘now’) with an explicit timezone of UTC, it was necessary to pass both arguments in order to provide the second timezone argument, even though we want the default value for the first argument: Public DateTimeImmutable::_construct(string $datetime = "now", ?DateTimeZone $timezone = null) It’s one of those features that I love as an end user developer although it can be a nightmare for library and framework developers, because argument names are now part of the API for public methods, not simply the argument types, and any change to an argument name becomes a backward-compatible break.īefore the introduction of named arguments, PHP required function and method arguments to be passed by their order according to the function/method signature: so for a method like One of the new features of PHP 8 is named arguments.
