-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add support for "write-once" properties #5186
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d340d84
Add support for final properties
kocsismate df0b729
Address some review comments
kocsismate 9b82621
Another round of review
kocsismate a79362a
Add more tests for read access
kocsismate 026192e
Implement check for combined assignment operators
kocsismate d73f5a5
Try to make unsetting array items work + add tests for pass by ref
kocsismate 21f8405
Add some tests for serialization
kocsismate 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 was deleted.
Oops, something went wrong.
64 changes: 64 additions & 0 deletions
64
Zend/tests/final_properties/access/lazy_initialization.phpt
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,64 @@ | ||
--TEST-- | ||
Test that final properties can be initialized lazily by unsetting them and using __get() | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
final public int $property1; | ||
final public string $property2; | ||
final public stdClass $property3; | ||
} | ||
|
||
class Bar extends Foo | ||
{ | ||
private $lazyProperty1; | ||
private $lazyProperty2; | ||
private $lazyProperty3; | ||
|
||
public function __construct() | ||
{ | ||
unset($this->property1); | ||
unset($this->property2); | ||
unset($this->property3); | ||
} | ||
|
||
public function __get($name) | ||
{ | ||
switch ($name) { | ||
case "property1": | ||
if ($this->lazyProperty1 === null) { | ||
$this->lazyProperty1 = 1; | ||
} | ||
|
||
return $this->lazyProperty1; | ||
case "property2": | ||
if ($this->lazyProperty2 === null) { | ||
$this->lazyProperty2 = "foo"; | ||
} | ||
|
||
return $this->lazyProperty2; | ||
case "property3": | ||
if ($this->lazyProperty3 === null) { | ||
$this->lazyProperty3 = new stdClass(); | ||
} | ||
|
||
return $this->lazyProperty3; | ||
default: | ||
throw new Exception(); | ||
} | ||
} | ||
} | ||
|
||
$foo = new Bar(); | ||
|
||
var_dump($foo->property1); | ||
var_dump($foo->property2); | ||
var_dump($foo->property3); | ||
|
||
?> | ||
--EXPECT-- | ||
int(1) | ||
string(3) "foo" | ||
object(stdClass)#2 (0) { | ||
} |
52 changes: 52 additions & 0 deletions
52
Zend/tests/final_properties/access/property_array_access.phpt
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,52 @@ | ||
--TEST-- | ||
Test that ArrayAccess works for final properties as expected | ||
--FILE-- | ||
<?php | ||
|
||
class Foo implements ArrayAccess | ||
{ | ||
final private string $property1; | ||
final private string $property2 = ""; | ||
|
||
function offsetExists($offset) | ||
{ | ||
return isset($this->$offset); | ||
} | ||
|
||
function offsetGet($offset) | ||
{ | ||
return $this->$offset ?? null; | ||
} | ||
|
||
function offsetSet($offset, $value) | ||
{ | ||
$this->$offset = $value; | ||
} | ||
|
||
function offsetUnset($offset){ | ||
unset($this->$offset); | ||
} | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
$foo["property1"] = "foo"; | ||
|
||
try { | ||
$foo["property1"] = "foo"; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo["property2"] = "foo"; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
$foo["property3"] = "foo"; | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property2 after initialization |
28 changes: 28 additions & 0 deletions
28
Zend/tests/final_properties/access/property_array_functions.phpt
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-- | ||
Test that final properties can be used with key() and current(), but not with next() | ||
--XFAIL-- | ||
Incorrect error message | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
final public array $property1 = ["foo", "bar", "baz"]; | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
var_dump(current($foo->property1)); | ||
var_dump(key($foo->property1)); | ||
|
||
try { | ||
next($foo->property1); | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
string(3) "foo" | ||
int(0) | ||
Cannot modify final property Foo::$property1 after initialization |
104 changes: 104 additions & 0 deletions
104
Zend/tests/final_properties/access/property_assignment1.phpt
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,104 @@ | ||
--TEST-- | ||
Test that final properties can't be mutated by assignment operators | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
final public int $property1 = 1; | ||
final public string $property2 = ""; | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
try { | ||
$foo->property1 = $foo->property1 + 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 - 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 * 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 / 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 ** 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 & 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 | 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 ^ 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 << 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property1 = $foo->property1 >> 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property2 = $foo->property2 . "foo"; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
$foo->property2 = $foo->property2 ?? "foo"; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
var_dump($foo->property1); | ||
var_dump($foo->property2); | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property1 after initialization | ||
Cannot modify final property Foo::$property2 after initialization | ||
Cannot modify final property Foo::$property2 after initialization | ||
int(1) | ||
string(0) "" |
104 changes: 104 additions & 0 deletions
104
Zend/tests/final_properties/access/property_assignment2.phpt
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,104 @@ | ||
--TEST-- | ||
Test that final static properties can't be mutated by assignment operators | ||
--FILE-- | ||
<?php | ||
|
||
class Foo | ||
{ | ||
final public static int $property1 = 1; | ||
final public static string $property2 = ""; | ||
} | ||
|
||
$foo = new Foo(); | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 + 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 - 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 * 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 / 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 ** 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 & 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 | 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 ^ 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 << 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property1 = Foo::$property1 >> 1; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property2 = Foo::$property2 . "foo"; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
try { | ||
Foo::$property2 = Foo::$property2 ?? "foo"; | ||
} catch (Error $exception) { | ||
echo $exception->getMessage() . "\n"; | ||
} | ||
|
||
var_dump( Foo::$property1); | ||
var_dump( Foo::$property2); | ||
|
||
?> | ||
--EXPECT-- | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property1 after initialization | ||
Cannot modify final static property Foo::$property2 after initialization | ||
Cannot modify final static property Foo::$property2 after initialization | ||
int(1) | ||
string(0) "" |
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.
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.
These tests might not be very useful because all of them boil down to simple assignments from the perspective of "write-once" properties