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