Skip to content

Commit 184c879

Browse files
committed
Address some review comments
1 parent d48bdc5 commit 184c879

21 files changed

+222
-179
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
--TEST--
2+
Test that final properties can be initialized lazily by unsetting them and using __get()
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final public int $property1;
9+
final public string $property2;
10+
final public stdClass $property3;
11+
}
12+
13+
class Bar extends Foo
14+
{
15+
private $lazyProperty1;
16+
private $lazyProperty2;
17+
private $lazyProperty3;
18+
19+
public function __construct()
20+
{
21+
unset($this->property1);
22+
unset($this->property2);
23+
unset($this->property3);
24+
}
25+
26+
public function __get($name)
27+
{
28+
switch ($name) {
29+
case "property1":
30+
if ($this->lazyProperty1 === null) {
31+
$this->lazyProperty1 = 1;
32+
}
33+
34+
return $this->lazyProperty1;
35+
case "property2":
36+
if ($this->lazyProperty2 === null) {
37+
$this->lazyProperty2 = "foo";
38+
}
39+
40+
return $this->lazyProperty2;
41+
case "property3":
42+
if ($this->lazyProperty3 === null) {
43+
$this->lazyProperty3 = new stdClass();
44+
}
45+
46+
return $this->lazyProperty3;
47+
default:
48+
throw new Exception();
49+
}
50+
}
51+
}
52+
53+
$foo = new Bar();
54+
55+
var_dump($foo->property1);
56+
var_dump($foo->property2);
57+
var_dump($foo->property3);
58+
59+
?>
60+
--EXPECT--
61+
int(1)
62+
string(3) "foo"
63+
object(stdClass)#2 (0) {
64+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
Test that declared final properties can't be accessed by reference
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final public int $property1;
9+
final public int $property2 = 2;
10+
11+
public function __construct()
12+
{
13+
$this->property1 = 1;
14+
}
15+
}
16+
17+
$foo = new Foo();
18+
19+
try {
20+
foreach ($foo as &$property) {}
21+
} catch (Error $exception) {
22+
echo $exception->getMessage() . "\n";
23+
}
24+
25+
?>
26+
--EXPECT--
27+
Cannot acquire reference on final property Foo::$property1

Zend/tests/final_properties/access/property_incrementing_decrementing_by_ref.phpt

Lines changed: 0 additions & 80 deletions
This file was deleted.

Zend/tests/final_properties/access/property_mutation_by_ref2.phpt

Lines changed: 0 additions & 37 deletions
This file was deleted.

Zend/tests/final_properties/clone/clone_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Test that final properties can't be modified after cloning
2+
Test that initialized final properties can't be modified after cloning
33
--FILE--
44
<?php
55

Zend/tests/final_properties/clone/clone_success.phpt renamed to Zend/tests/final_properties/clone/clone_success1.phpt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ Test that final properties can be cloned
66
class Foo
77
{
88
final public int $property1;
9-
final public string $property2 = "";
9+
final public int $property2;
10+
final public string $property3 = "";
1011
}
1112

1213
$foo = new Foo();
13-
$foo->property1 = 1;
14+
$foo->property2 = 2;
1415
var_dump($foo);
1516
$bar = clone $foo;
1617
var_dump($bar);
@@ -19,13 +20,17 @@ var_dump($bar);
1920
--EXPECTF--
2021
object(Foo)#1 (2) {
2122
["property1"]=>
22-
int(1)
23+
uninitialized(int)
2324
["property2"]=>
25+
int(2)
26+
["property3"]=>
2427
string(0) ""
2528
}
2629
object(Foo)#2 (2) {
2730
["property1"]=>
28-
int(1)
31+
uninitialized(int)
2932
["property2"]=>
33+
int(2)
34+
["property3"]=>
3035
string(0) ""
3136
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Test that uninitialized final properties can be initialized after cloning
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
final public int $property1;
9+
}
10+
11+
$foo = new Foo();
12+
var_dump($foo);
13+
$bar = clone $foo;
14+
$bar->property1 = 1;
15+
var_dump($bar);
16+
17+
?>
18+
--EXPECTF--
19+
object(Foo)#1 (0) {
20+
["property1"]=>
21+
uninitialized(int)
22+
}
23+
object(Foo)#2 (1) {
24+
["property1"]=>
25+
int(1)
26+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that non-private final properties can't override non-final properties
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
protected int $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
final protected int $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Cannot redeclare non-final property Foo::$property1 as final Bar::$property1 in %s on line %d
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Test that non-private final static properties can't override non-final static properties
3+
--FILE--
4+
<?php
5+
6+
class Foo
7+
{
8+
protected static int $property1;
9+
}
10+
11+
class Bar extends Foo
12+
{
13+
final protected static int $property1;
14+
}
15+
16+
?>
17+
--EXPECTF--
18+
Fatal error: Cannot redeclare non-final property Foo::$property1 as final Bar::$property1 in %s on line %d

Zend/tests/final_properties/inheritance/inheritance_success1.phpt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
--TEST--
2-
Test that final properties can be overwritten by non-final properties
2+
Test that non-final properties can override final properties
33
--FILE--
44
<?php
55

66
class Foo
77
{
8-
final protected static int $property1;
9-
final protected int $property2;
8+
final private static int $property1;
9+
final private int $property2;
10+
11+
final protected static int $property3;
12+
final protected int $property4;
1013
}
1114

1215
class Bar extends Foo
1316
{
14-
protected static int $property1;
15-
protected int $property2;
17+
final private static int $property1;
18+
final private int $property2;
19+
20+
protected static int $property3;
21+
protected int $property4;
1622
}
1723

1824
?>

Zend/tests/final_properties/inheritance/inheritance_success2.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
--TEST--
2-
Test that non-final properties can be overwritten by final properties
2+
Test that private final properties can override non-final properties
33
--FILE--
44
<?php
55

66
class Foo
77
{
8-
protected static int $property1;
9-
protected int $property2;
8+
private static int $property1;
9+
private int $property2;
1010
}
1111

1212
class Bar extends Foo
1313
{
14-
final protected static int $property1;
15-
final protected int $property2;
14+
final private static int $property1;
15+
final private int $property2;
1616
}
1717

1818
?>

Zend/tests/final_properties/unset/declared_unset_error.phpt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ try {
2323
echo $exception->getMessage() . "\n";
2424
}
2525

26-
// Uninitialized typed properties can't be unset
2726
unset($foo->property2);
2827
unset($foo->property2);
2928

0 commit comments

Comments
 (0)