-
Notifications
You must be signed in to change notification settings - Fork 7.9k
RFC: Locked Classes #3931
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
Closed
Closed
RFC: Locked Classes #3931
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ef92bb8
Implement "locked" classes, using a new semi-reserved keyword
IMSoP 5ec0a69
Add reflection information for locked classes
IMSoP 3ff7f5d
Add PHPT tests covering basic features of locked classes
IMSoP 02c4fa3
Ensure __get, __set, and __unset will still be called on locked classes
IMSoP 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
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
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||
--TEST-- | ||||||
A sub-class of a locked class is not locked unless explciitly marked | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
--FILE-- | ||||||
<?php | ||||||
|
||||||
locked class Base { | ||||||
public $definedProp; | ||||||
} | ||||||
class UnlockedDerived extends Base { | ||||||
} | ||||||
locked class LockedDerived extends Base { | ||||||
} | ||||||
|
||||||
$t = new UnlockedDerived(); | ||||||
|
||||||
$t->definedProp = "OK\n"; | ||||||
echo $t->definedProp; | ||||||
unset($t->definedProp); | ||||||
|
||||||
$t->nonExistentProp = "Also OK\n"; | ||||||
echo $t->nonExistentProp; | ||||||
unset($t->nonExistentProp); | ||||||
|
||||||
|
||||||
$t = new LockedDerived(); | ||||||
|
||||||
$t->definedProp = "OK\n"; | ||||||
$t->nonExistentProp = "Not OK\n"; | ||||||
|
||||||
|
||||||
echo "Done\n"; // shouldn't be displayed | ||||||
?> | ||||||
--EXPECTF-- | ||||||
OK | ||||||
Also OK | ||||||
|
||||||
Fatal error: Uncaught Error: Cannot write undefined property %s on locked class %s in %s:%d | ||||||
Stack trace: | ||||||
#0 {main} | ||||||
thrown 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,25 @@ | ||
--TEST-- | ||
A locked class prevents undefined properties being accessed | ||
--FILE-- | ||
<?php | ||
|
||
locked class TestClass { | ||
public $definedProp; | ||
} | ||
|
||
$t = new testClass(); | ||
|
||
$t->definedProp = "OK\n"; | ||
echo $t->definedProp; | ||
|
||
echo $t->nonExistentProp; | ||
|
||
echo "Done\n"; // shouldn't be displayed | ||
?> | ||
--EXPECTF-- | ||
OK | ||
|
||
Fatal error: Uncaught Error: Cannot access undefined property %s on locked class %s in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown 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,30 @@ | ||
--TEST-- | ||
A locked class may still have __get overload | ||
--FILE-- | ||
<?php | ||
|
||
locked class TestClass { | ||
public $definedProp; | ||
private $privateProp; | ||
|
||
public function __get($prop) { | ||
return "__get called for $prop\n"; | ||
} | ||
} | ||
|
||
$t = new testClass(); | ||
|
||
$t->definedProp = "OK\n"; | ||
echo $t->definedProp; | ||
|
||
echo $t->privateProp; | ||
echo $t->nonExistentProp; | ||
|
||
echo "Done\n"; | ||
?> | ||
--EXPECT-- | ||
OK | ||
__get called for privateProp | ||
__get called for nonExistentProp | ||
Done | ||
|
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,29 @@ | ||
--TEST-- | ||
A locked class cannot set undefined properties on its own instances | ||
--FILE-- | ||
<?php | ||
|
||
locked class TestClass { | ||
private $definedProp; | ||
|
||
public function test() { | ||
$this->definedProp = "OK\n"; | ||
echo $this->definedProp; | ||
|
||
$this->nonExistentProp = "Not OK\n"; | ||
} | ||
} | ||
|
||
$t = new testClass(); | ||
$t->test(); | ||
|
||
echo "Done\n"; // shouldn't be displayed | ||
?> | ||
--EXPECTF-- | ||
OK | ||
|
||
Fatal error: Uncaught Error: Cannot write undefined property %s on locked class %s in %s:%d | ||
Stack trace: | ||
#0 %s(%d): TestClass->test() | ||
#1 {main} | ||
thrown 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,25 @@ | ||
--TEST-- | ||
A locked class prevents undefined properties being set | ||
--FILE-- | ||
<?php | ||
|
||
locked class TestClass { | ||
public $definedProp; | ||
} | ||
|
||
$t = new testClass(); | ||
|
||
$t->definedProp = "OK\n"; | ||
echo $t->definedProp; | ||
|
||
$t->nonExistentProp = "Not OK\n"; | ||
|
||
echo "Done\n"; // shouldn't be displayed | ||
?> | ||
--EXPECTF-- | ||
OK | ||
|
||
Fatal error: Uncaught Error: Cannot write undefined property %s on locked class %s in %s:%d | ||
Stack trace: | ||
#0 {main} | ||
thrown in %s on line %d |
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.
Since this constant will be exposed to userland (via
ReflectionClass::IS_LOCKED
), whereas most of these aren't, such a high number might seem surprising to users, but I'm not sure of the impact of rearranging this list.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.
We don't make any particularly guarantees about the values of those flags in reflection (in fact, they change between releases). I wouldn't worry about this.