Opting out of Named Parameters in PHP 8.0

Published On16 Nov 2020

@no-named-parameters DocBlock attribute

PHP 8.0 brings Named Parameters support to PHP.

With named parameters, the names of the function/method names become part of the API, and changing the parameter name will be breaking change.


function findSomething(string $heystack, string $needle) {}

With a function declaration like this, named parameter allows to call the findSomething function with explicitly declaring the parameters, without having to depend on the order of parameters.

findSomething(heystack: "FooBar", needle: "Bar");

Now, if the author of the findSomething notices the typo in heystack word, and decides to fix it with haystack, all code that used named parameters feature will raise a fatal error:

function findSomething(string $haystack, string $needle) {}
findSomething(heystack: "FooBar", needle: "Bar");
Fatal error: Uncaught Error: Unknown named parameter $heystack in ...:...

Note that in case of an interface/inheritance chain, PHP considers parameter names of the called class and all its parents.


@no-named-arguments

Over at Roave/BackwardCompatibilityCheck project, a discussion lead to a new @no-named-arguments DocBlock attribute (which is not the same as PHP 8.0 attributes as it is a PHP 8.0 feature) that might add support to explicitly declare that the code does not provide backwards-compatibility promise for parameter names, discouraging the use of named parameters with it.

If this DocBlock attribute is present, it means the author explicitly declares that there will be no backwards compatibility promise for the parameter names, and users of the library should not use named parameters calling pattern for functions/methods from that library.

/**
 * @no-named-arguments
 */
function findSomething(string $haystack, string $needle) {}

It is also possible to set a custom description:

/**
 * @no-named-arguments No BC promise.
 */
function findSomething(string $haystack, string $needle) {}

Adoption of @no-named-arguments

Static Analyzers

Projects

From the top 100 most downloaded Composer projects:

  • PHPUnit: Parameter names are not covered by the backward compatibility promise for PHPUnit

IDEs

PHPStorm will support named parameters in PHPStorm 2020.3.

Named parameters in PHPStorm 2020.3

It appears that there is no support for @no-named-arguments attribute in PHPStorm or any other IDE yet.

Recent Articles on PHP.Watch

All ArticlesFeed 
PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

A guide for Debian and Ubuntu on how to install PHP 8.4 on a new server or how to upgrade an existing PHP setup to PHP 8.4.
How to fix `mysql_native_password` not loaded errors on MySQL 8.4

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to fix the SQLSTATE[HY000] [1524] Plugin 'mysql_native_password' is not loaded errors caused in MySQL 8.4 no longer enabling the mysql_native_password plugin by default.
How to fix PHP Curl HTTPS Certificate Authority issues on Windows

How to fix PHP Curl HTTPS Certificate Authority issues on Windows

On Windows, HTTPS requests made with the Curl extension can fail because Curl has no root certificate list to validate the server certificates. This article discusses the secure and effective solutions, and highlights bad advice that can leave PHP applications insecure.
Subscribe to PHP.Watch newsletter for monthly updates

You will receive an email on last Wednesday of every month and on major PHP releases with new articles related to PHP, upcoming changes, new features and what's changing in the language. No marketing emails, no selling of your contacts, no click-tracking, and one-click instant unsubscribe from any email you receive.

Support PHP.Watch — If you find the articles, version information, Codex, and other PHP.Watch contributions useful, consider supporting through GitHub Sponsors. Your sponsorship helps dedicate more time to creating valuable content and improving the PHP community. Together, we can keep the momentum going — thank you for your support!

Thanks to the highest tier sponsor: @TomasVotruba for your generous support to keep PHP.Watch moving 💜