Skip to content

[RFC] Final Property Promotion #17861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 22, 2025

Conversation

DanielEScherzer
Copy link
Member

@DanielEScherzer DanielEScherzer commented Feb 19, 2025

@TimWolla
Copy link
Member

This does not appear to be explicitly specified in the RFC and is a language change. I'm not sure this is an appropriate bugfix for 8.4.

@TimWolla
Copy link
Member

/cc @Crell

@iluuu1994
Copy link
Member

This came up, and there were some concerns it could imply const parameters as in Java. So we just left it out.

@iluuu1994
Copy link
Member

So no, this is not a bug. If you think final should be a promoted keyword, please open a new discussion on the mailing list.

@iluuu1994 iluuu1994 closed this Feb 19, 2025
@iluuu1994 iluuu1994 reopened this Mar 21, 2025
@DanielEScherzer DanielEScherzer changed the base branch from PHP-8.4 to master March 24, 2025 18:26
@DanielEScherzer DanielEScherzer changed the title GH-17860: allow final in constructor property promotion [RFC] Final Property Promotion Mar 24, 2025
Comment on lines +12 to +14
class B extends A {
public $prop { get {} set {} }
}
Copy link
Contributor

@mvorisek mvorisek Apr 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class B extends A {
public $prop { get {} set {} }
}
class B extends A {
public $prop = 5
}
Suggested change
class B extends A {
public $prop { get {} set {} }
}
class B extends A {
public function __construct(
public $prop
) {}
}
Suggested change
class B extends A {
public $prop { get {} set {} }
}
class B extends A {
public function __construct(
$prop
) {}
}

Is this allowed after this PR? If yes, is it tested?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub's display is a bit confusing - are you suggesting that the code be

class B extends A {
    $prop
}

? That isn't valid PHP

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(edited)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class B extends A {
    public function __construct(
        $prop
    ) {}
}

doesn't involve property promotion

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does in A class and I would like to get this tested.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added more tests to confirm that there isn't an issue with this

@@ -12,6 +12,8 @@ class C {
public protected(set) final mixed $p6;
public private(set) mixed $p7;
public private(set) final mixed $p8;

public function __construct( final $p9 ) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    public function __construct( $p9 ) {}

It would be good to test also the case without the final property in the constructor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@iluuu1994 iluuu1994 linked an issue Jun 19, 2025 that may be closed by this pull request
@DanielEScherzer
Copy link
Member Author

DanielEScherzer commented Jun 20, 2025

Any more reviewers? If not I'll probably merge this next week

@TimWolla TimWolla self-requested a review June 21, 2025 09:43
Copy link
Member

@iluuu1994 iluuu1994 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only nits, the implementation looks correct. Thanks @DanielEScherzer!

Copy link
Member

@TimWolla TimWolla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to also add NEWS before merging.

@DanielEScherzer DanielEScherzer merged commit 4dfba7a into php:master Jun 22, 2025
9 checks passed
@DanielEScherzer DanielEScherzer deleted the final-promoted-props branch June 22, 2025 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow constructor property promotion with final properties
5 participants