1--TEST--
2Lazy objects: unset of undefined dynamic property initializes object (2)
3--FILE--
4<?php
5
6#[AllowDynamicProperties]
7class C {
8    public int $b = 1;
9
10    public function __construct(int $a) {
11        var_dump(__METHOD__);
12        $this->b = 2;
13    }
14}
15
16function test(string $name, object $obj) {
17    printf("# %s:\n", $name);
18
19    // no var_dump($obj), so that properties ht is not initialized
20    var_dump('before unset');
21    unset($obj->a);
22    var_dump($obj);
23}
24
25$reflector = new ReflectionClass(C::class);
26
27$obj = $reflector->newLazyGhost(function ($obj) {
28    var_dump("initializer");
29    $obj->__construct(1);
30});
31
32test('Ghost', $obj);
33
34$obj = $reflector->newLazyProxy(function ($obj) {
35    var_dump("initializer");
36    return new C(1);
37});
38
39test('Proxy', $obj);
40
41--EXPECTF--
42# Ghost:
43string(12) "before unset"
44string(11) "initializer"
45string(14) "C::__construct"
46object(C)#%d (1) {
47  ["b"]=>
48  int(2)
49}
50# Proxy:
51string(12) "before unset"
52string(11) "initializer"
53string(14) "C::__construct"
54lazy proxy object(C)#%d (1) {
55  ["instance"]=>
56  object(C)#%d (1) {
57    ["b"]=>
58    int(2)
59  }
60}
61