====== PHP RFC: #[\Deprecated] on optional parameters ====== * Version: 0.9 * Date: 2026-12-06 * Author: Weilin Du, weilindu@php.net * Status: Draft * Implementation: https://github.com/LamentXU123/php-src/tree/deprecated-optional-parameters * 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: 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. 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. '']); // 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. 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: 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. [[https://github.com/php/policies/blob/main/feature-proposals.rst#voting-phase|PHP voting rules]] * Yes * No * Abstain ===== Patches and Tests ===== This is implemented in my branch https://github.com/LamentXU123/php-src/tree/deprecated-optional-parameters in commit ''4c4b21be549''. ===== References ===== * [[https://wiki.php.net/rfc/deprecated_attribute|Original Deprecated attribute RFC]] * [[https://externals.io/message/123184#123333|Suggestion to allow the attribute on parameters]] and [[https://externals.io/message/123184#123334|reply]], May 2024 * [[https://wiki.php.net/rfc/partial_function_application_v2|Partial Function Application RFC]] ===== Changelog ===== * 2026-12-06: Initial draft.