-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[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
DanielEScherzer
merged 5 commits into
php:master
from
DanielEScherzer:final-promoted-props
Jun 22, 2025
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cca1e8d
[RFC] Final Property Promotion
DanielEScherzer 6475a39
Add a few more tests
DanielEScherzer b541af1
Another ReflectionProperty::isFinal() case
DanielEScherzer 1967a7b
Remove extra spaces
DanielEScherzer fb670f0
Update NEWS
DanielEScherzer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Promoted property may be marked final (hook) | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public function __construct( | ||
public final $prop { get {} set {} } | ||
) {} | ||
} | ||
|
||
class B extends A { | ||
public $prop { get {} set {} } | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot override final property A::$prop in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Promoted property may be marked final (normal) | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public function __construct( | ||
public final $prop | ||
) {} | ||
} | ||
|
||
class B extends A { | ||
public $prop { get {} set {} } | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot override final property A::$prop in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Promoted property may be marked final (no visibility needed) | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public function __construct( | ||
final $prop | ||
) {} | ||
} | ||
|
||
class B extends A { | ||
public $prop { get {} set {} } | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot override final property A::$prop in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Final promoted property conflicts with non-promoted non-hooked property | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public function __construct( | ||
final $prop | ||
) {} | ||
} | ||
|
||
class B extends A { | ||
public $prop; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot override final property A::$prop in %s on line %d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Non-promoted constructor parameter does not conflict with final promoted property | ||
--FILE-- | ||
<?php | ||
|
||
class A { | ||
public function __construct( | ||
final $prop | ||
) { | ||
echo __METHOD__ . "(): $prop\n"; | ||
} | ||
} | ||
|
||
class B extends A { | ||
public function __construct( | ||
$prop | ||
) { | ||
echo __METHOD__ . "(): $prop\n"; | ||
parent::__construct($prop); | ||
} | ||
} | ||
|
||
$b = new B("test" ); | ||
DanielEScherzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
?> | ||
--EXPECT-- | ||
B::__construct(): test | ||
A::__construct(): test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--TEST-- | ||
Confirm that the AST indicates final promoted properties | ||
--FILE-- | ||
<?php | ||
try { | ||
assert(false && new class { | ||
public function __construct( public final $prop ) {} | ||
DanielEScherzer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} catch (Error $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
?> | ||
--EXPECT-- | ||
assert(false && new class { | ||
public function __construct(public final $prop) { | ||
} | ||
|
||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this allowed after this PR? If yes, is it tested?
There was a problem hiding this comment.
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
? That isn't valid PHP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(edited)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't involve property promotion
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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