1
1
--TEST--
2
- Readonly hooks may be lazy loaded
2
+ Readonly classes can be constructed via reflection by ORM
3
3
--FILE--
4
4
<?php
5
5
@@ -18,9 +18,19 @@ class MockDbConnection implements DbConnection {
18
18
}
19
19
}
20
20
21
- readonly class LazyProduct
21
+ readonly class Product
22
+ {
23
+ public function __construct (
24
+ public string $ name ,
25
+ public float $ price ,
26
+ public Category $ category ,
27
+ ) {}
28
+ }
29
+
30
+ readonly class LazyProduct extends Product
22
31
{
23
32
private DbConnection $ dbApi ;
33
+
24
34
private string $ categoryId ;
25
35
26
36
public Category $ category {
@@ -30,8 +40,16 @@ readonly class LazyProduct
30
40
}
31
41
}
32
42
33
- $ product = new LazyProduct ();
34
- $ reflect = new ReflectionClass ($ product );
43
+ $ reflect = new ReflectionClass (LazyProduct::class);
44
+ $ product = $ reflect ->newInstanceWithoutConstructor ();
45
+
46
+ $ nameProperty = $ reflect ->getProperty ('name ' );
47
+ $ nameProperty ->setAccessible (true );
48
+ $ nameProperty ->setValue ($ product , 'Iced Chocolate ' );
49
+
50
+ $ priceProperty = $ reflect ->getProperty ('price ' );
51
+ $ priceProperty ->setAccessible (true );
52
+ $ priceProperty ->setValue ($ product , 1.99 );
35
53
36
54
$ db = $ reflect ->getProperty ('dbApi ' );
37
55
$ db ->setAccessible (true );
@@ -52,6 +70,9 @@ echo $category2->name . "\n";
52
70
// same category instance returned
53
71
var_dump ($ category1 === $ category2 );
54
72
73
+ // can't be wrong, huh?
74
+ var_dump ($ product );
75
+
55
76
// cannot set twice
56
77
try {
57
78
$ categoryId ->setValue ($ product , '420 ' );
@@ -65,4 +86,20 @@ hit database
65
86
Category 42
66
87
Category 42
67
88
bool(true)
89
+ object(LazyProduct)#2 (5) {
90
+ ["name"]=>
91
+ string(14) "Iced Chocolate"
92
+ ["price"]=>
93
+ float(1.99)
94
+ ["category"]=>
95
+ object(Category)#8 (1) {
96
+ ["name"]=>
97
+ string(11) "Category 42"
98
+ }
99
+ ["dbApi":"LazyProduct":private]=>
100
+ object(MockDbConnection)#6 (0) {
101
+ }
102
+ ["categoryId":"LazyProduct":private]=>
103
+ string(2) "42"
104
+ }
68
105
Cannot modify readonly property LazyProduct::$categoryId
0 commit comments