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