Skip to content

Commit 41a8aba

Browse files
committed

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

Zend/tests/property_hooks/readonly_lazy.phpt

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
Readonly hooks may be lazy loaded
2+
Readonly classes can be constructed via reflection by ORM
33
--FILE--
44
<?php
55

@@ -18,9 +18,19 @@ class MockDbConnection implements DbConnection {
1818
}
1919
}
2020

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
2231
{
2332
private DbConnection $dbApi;
33+
2434
private string $categoryId;
2535

2636
public Category $category {
@@ -30,8 +40,16 @@ readonly class LazyProduct
3040
}
3141
}
3242

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);
3553

3654
$db = $reflect->getProperty('dbApi');
3755
$db->setAccessible(true);
@@ -52,6 +70,9 @@ echo $category2->name . "\n";
5270
// same category instance returned
5371
var_dump($category1 === $category2);
5472

73+
// can't be wrong, huh?
74+
var_dump($product);
75+
5576
// cannot set twice
5677
try {
5778
$categoryId->setValue($product, '420');
@@ -65,4 +86,20 @@ hit database
6586
Category 42
6687
Category 42
6788
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+
}
68105
Cannot modify readonly property LazyProduct::$categoryId

0 commit comments

Comments
 (0)