PHP RFC: #[\Deprecated] on optional parameters
- Version: 0.9
- Date: 2026-12-06
- Author: Weilin Du, weilindu@php.net
- Status: Draft
- Discussion thread: tbd
- Voting thread: tbd
Introduction
The library might wish to remove an optional parameter but still keep the function around. The RFC permits the parameter to be marked as #[\Deprecated]. A deprecation is issued by PHP only if the parameter is provided by the caller.
Consider this short example:
<?php function writeLine( string $text, #[\Deprecated(message: 'include the prefix in $text instead', since: '2.4')] string $prefix = '', string $ending = "\n", ): void { echo $prefix, $text, $ending; } writeLine('Ready', ending: "\r\n"); // Ready writeLine('Ready', '', "\r\n"); // Deprecated: writeLine(): Passing argument #2 ($prefix) is deprecated since 2.4, include the prefix in $text instead in %s on line %d // Ready
Both lines will output the same line of text. In the first example, the $prefix is bypassed since $ending is specified. In the second case, an empty value is passed to $prefix, and as a result, PHP issues a deprecation notice before printing the line.
Proposal
#[\Deprecated] can be used for an optional argument to a function or method call, even an argument passed by reference. Using it on a required or variadic argument is a compilation error. This includes an argument which PHP considers required because there is a required argument after it. When using #[\DelayedTargetValidation] in addition, ReflectionAttribute::newInstance() throws the error on attempting to create an instance of the invalid attribute.
<?php function deliver(#[\DelayedTargetValidation, \Deprecated] string $text): void {} deliver('Ready'); // No notice. $parameter = new ReflectionParameter('deliver', 'text'); $attribute = $parameter->getAttributes(\Deprecated::class)[0]; try { $attribute->newInstance(); } catch (Error $e) { echo $e->getMessage(), "\n"; } // Cannot apply #[\Deprecated] to a required or variadic parameter
It is possible to generate the deprecation via positional, named, or unpacked arguments. The same is true for calls made via reflection or callable. The construction of a first-class callable or partial application is silent. However, when calling the partial, a deprecation will be generated when the argument was specified either during its construction or call time.
<?php function sendLine(string $text, #[\Deprecated] string $prefix = ''): void {} $withPrefix = sendLine(?, '[app] '); // No notice when binding $prefix. $withPrefix('Ready'); // Deprecated: sendLine(): Passing argument #2 ($prefix) is deprecated $ready = sendLine('Ready', ...); $ready(); // No notice when $prefix is omitted. $ready(...['prefix' => '']); // Deprecated: sendLine(): Passing argument #2 ($prefix) is deprecated
Every deprecated parameter provided gives rise to a notice, in the order in which the parameters are defined. In case the whole function is deprecated, then its notice will be thrown first. Any invalid invocation, such as providing two identical named arguments, will produce the usual argument error first. In case the deprecation produces an exception in the error handling routine, then the function body will never execute. If the function is a generator, then the client of that function will receive a notice at the time of invocation of that function.
<?php function send(#[\Deprecated] bool $urgent = false): void { echo "Sent\n"; } set_error_handler(static function (int $level, string $message) { throw new ErrorException($message, 0, $level); }, E_USER_DEPRECATED); try { send(urgent: false); } catch (ErrorException $e) { echo $e->getMessage(), "\n"; } // send(): Passing argument #1 ($urgent) is deprecated
Notice contains the function/method name, position of the parameter and the parameter name. The optional since and message parameters are given above. PHP provides E_USER_DEPRECATED for user-defined functions/methods and E_DEPRECATED for internal ones.
Methods and constructors
Inheritance will make use of the parameters in terms of attributes that the method being inherited possesses. On the other hand, override makes use of the attributes in its own parameters. In addition, an attribute of an interface or abstract method is visible through reflection in that particular declaration. The concrete implementation of the method issues a warning only if the parameter is annotated.
The deprecation of a promoted constructor parameter is applicable when the constructor argument is passed. For example:
<?php class Options { public function __construct(#[\Deprecated] public bool $legacy = false) {} } $options = new Options(); // No notice. $options->legacy = true; // No notice. var_dump($options->legacy); // bool(true) new Options(legacy: false); // Deprecated: Options::__construct(): Passing argument #1 ($legacy) is deprecated in %s on line %d
Backward Incompatible Changes
None.
Proposed PHP Version(s)
PHP 8.7.
RFC Impact
Extensions can use the attribute on their own optional parameters. PHP users can identify deprecated arguments at the call site. The attribute and its arguments are available through ReflectionParameter::getAttributes(\Deprecated::class).
Voting Choices
Adoption requires a two-thirds majority of Yes and No votes. PHP voting rules
Patches and Tests
This is implemented in my branch https://github.com/LamentXU123/php-src/tree/deprecated-optional-parameters in commit 4c4b21be549.
References
- Suggestion to allow the attribute on parameters and reply, May 2024
Changelog
- 2026-12-06: Initial draft.