11--TEST--
2- Promoted readonly property reassignment in constructor - indirect reassignment not allowed
2+ Promoted readonly property reassignment in constructor - indirect reassignment allowed
33--FILE--
44<?php
55
6- // Reassignment is NOT allowed in methods called by the constructor
6+ // Reassignment IS allowed in methods called by the constructor
77class CalledMethod {
88 public function __construct (
99 public readonly string $ prop = 'default ' ,
1010 ) {
11- try {
12- $ this ->initProp ();
13- } catch (Error $ e ) {
14- echo $ e ->getMessage (), "\n" ;
15- }
11+ $ this ->initProp ();
1612 }
1713
1814 private function initProp (): void {
@@ -23,28 +19,45 @@ class CalledMethod {
2319$ cm = new CalledMethod ();
2420var_dump ($ cm ->prop );
2521
26- // Reassignment is NOT allowed in closures called by the constructor
22+ // Reassignment IS allowed in closures called by the constructor
2723class ClosureInConstructor {
2824 public function __construct (
2925 public readonly string $ prop = 'default ' ,
3026 ) {
3127 $ fn = function () {
3228 $ this ->prop = 'from closure ' ;
3329 };
30+ $ fn ();
31+ }
32+ }
33+
34+ $ cc = new ClosureInConstructor ();
35+ var_dump ($ cc ->prop );
36+
37+ // But second reassignment still fails
38+ class MultipleReassign {
39+ public function __construct (
40+ public readonly string $ prop = 'default ' ,
41+ ) {
42+ $ this ->initProp ();
3443 try {
35- $ fn ();
44+ $ this -> initProp (); // Second call - should fail
3645 } catch (Error $ e ) {
3746 echo $ e ->getMessage (), "\n" ;
3847 }
3948 }
49+
50+ private function initProp (): void {
51+ $ this ->prop = 'from method ' ;
52+ }
4053}
4154
42- $ cc = new ClosureInConstructor ();
43- var_dump ($ cc ->prop );
55+ $ mr = new MultipleReassign ();
56+ var_dump ($ mr ->prop );
4457
4558?>
4659--EXPECT--
47- Cannot modify readonly property CalledMethod::$prop
48- string(7 ) "default "
49- Cannot modify readonly property ClosureInConstructor ::$prop
50- string(7 ) "default "
60+ string(11) "from method"
61+ string(12 ) "from closure "
62+ Cannot modify readonly property MultipleReassign ::$prop
63+ string(11 ) "from method "
0 commit comments