When property-hooks added property hooks, it also added a mechanism for properties to be declared as final
. However, the final
modifier is not currently supported for properties that are created via constructor property promotion. This RFC proposes that final
properties also be allowed in constructor property promotion.
The reasoning is basically the same as from the original constructor_promotion RFC - reducing boilderplate.
When a constructor parameter is marked with final
, it is considered “promoted” and treated like other promoted properties, with the resulting property also being marked as final
.
Since properties declared as final
do not need an explicit visibility set (defaulting to public), no visibility is required in the promotion if final
is used, though visibility (including asymmetric visibility) and other features (like property hooks) can be used and combined with final
.
None, all prior code that was valid is executed the same
As with all RFCs that make code that previously had a compile-time error become valid, this RFC requires updates to static analyzers and IDEs to understand the semantics of constructor parameters marked as final.
Next version of PHP (PHP 8.5 or PHP 9.0)
Since final
was not valid for property promotion previously, the behavior of existing legal code is unchanged.
Allow final
to be used with property promotion?
After the project is implemented, this section should contain