Skip to content

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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions Zend/tests/errmsg_038.phpt

This file was deleted.

64 changes: 64 additions & 0 deletions Zend/tests/final_properties/access/lazy_initialization.phpt
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 Zend/tests/final_properties/access/property_array_access.phpt
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 Zend/tests/final_properties/access/property_array_functions.phpt
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 Zend/tests/final_properties/access/property_assignment1.phpt
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
Copy link
Member Author

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

--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 Zend/tests/final_properties/access/property_assignment2.phpt
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) ""
Loading