rfc:final_promotion

PHP RFC: Final Property Promotion

Introduction

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.

Proposal

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.

Backward Incompatible Changes

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.

Proposed PHP Version(s)

Next version of PHP (PHP 8.5 or PHP 9.0)

Unaffected PHP Functionality

Since final was not valid for property promotion previously, the behavior of existing legal code is unchanged.

Proposed Voting Choices

Allow final to be used with property promotion?

Patches and Tests

Implementation

After the project is implemented, this section should contain

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature
  4. a link to the language specification section (if any)

References

rfc/final_promotion.txt · Last modified: 2025/03/24 18:27 by danielescherzer