This article is more than 1 year old

Named arguments squeak into PHP 8.0, 7 years after first RFC

How about inheritance? 'One of the bigger open questions' says author of proposal

The next major version of the PHP language will support named arguments after 76 per cent of lead developers voted to include it.

PHP 8.0 is now expected to be released in November 2020 and feature freeze is on 4 August. The RFC (Request For Comments) for Named Arguments was submitted in September 2013 but revived for the PHP 8.0 release. Approval required a two-thirds majority. There is a pull request for a partial implementation, but it seems there is a fair amount of work still to do.

Named arguments mean that instead of the arguments to a function being interpreted according to their position, there is an option to specify them by name. This improves the readability of code and can help avoid errors, especially where there are several arguments of the same type. Instead of:

add_to_basket($custid,$prodid,$qty);

You can write:

add_to_basket(customerID: $custid, productID: $prodid, quantity: $qty);

The RFC was originally put forward by Nikita Popov, who works on a PHP editor at tools company JetBrains. "If you have a method with, for example, three boolean arguments, that would be a really horrible method, because you call it like, true, true, false, like what does this mean? If you have named parameters, and you have the same three boolean arguments, then it's not really a problem any more," he said in a recent podcast (see transcript).

Named arguments are especially useful if you have the misfortune to be dealing with functions that have a long list of arguments. Named arguments can be specified in any order and it makes no difference. While they are in general more verbose, they can save some typing if you would otherwise have to include lots of empty strings or default values. The downside is the fact that changing the name of an argument will now break code.

There are also some complexities around inheritance. Popov explained: "This is one of the bigger open questions we have. The problem is that if you call a method with the names from the parent class, and the child class change them, then you'll get an error because this named parameter just doesn't exist in the child class.

And there are a couple of ways to approach that one is to forbid during inheritance, any kind of parameter name changes, which would be a fairly significant backwards break because, well, it never mattered in the past and based on some cursory analysis, this is like parameter name changes, somewhat common in code right now. The other possibility is to just ignore this issue, expect that a lot of code is never going to use named parameters."

According to the RFC, the intention is to "follow the model of Python or Ruby: PHP will silently accept parameter name changes during inheritance, which may result in call-time exceptions when methods with renamed parameters are called."

There is another possibility, though, which is to "allow the parameter names from both the parent method, and the child method," though Popov said this could be technically "a bit problematic." It is an example of the tricky issues language designers run into when adding features.

PHP will be joining a long list of languages that already have this feature, including C#, Kotlin, PowerShell, Python, Ruby, Swift and Visual Basic. Java and JavaScript do not support it, though.

Other new features in PHP 8.0 include a just-in-time compiler based on an extension called OPcache, Attributes, and new functions for searching strings: str_contains, str_starts_with() and str_ends_with(). ®

More about

TIP US OFF

Send us news


Other stories you might like