


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 I'm sure we'll see more and more built-in attributes in the future.One of the new features of PHP 8 is named arguments. One such example is the # attribute, and a popular example has been a # attribute - if you're not sure what that last one is about, you can read my post about what the JIT is. Once the base RFC had been accepted, new opportunities arose to add built-in attributes to the core. #[ AttributeWithBitShift( 4 > 1, 4 newInstance(), not earlier.

A list of everything that's allowed as a constant expression can be found in the source code.

This means that scalar expressions are allowed - even bit shifts - as well as ::class, constants, arrays and array unpacking, boolean expressions and the null coalescing operator. There's a little more to be said about this though: attributes only accept constant expressions as input arguments. # public function onProductCreated ( ProductCreated $event)Īnd can take no, one or several arguments, which are defined by the attribute's constructor: #Īs for allowed parameters you can pass to an attribute, you've already seen that class constants, ::class names and scalar types are allowed. First things first, here's what attribute would look like in the wild: use \ Support\ Attributes\ ListensTo
