1--TEST--
2Lazy objects: resetAsLazy*() calls destructor of pre-existing object
3--FILE--
4<?php
5
6class C {
7    public readonly int $a;
8
9    public function __construct() {
10        $this->a = 1;
11    }
12
13    public function __destruct() {
14        var_dump(__METHOD__);
15    }
16}
17
18$reflector = new ReflectionClass(C::class);
19
20print "# Ghost:\n";
21
22$obj = new C();
23print "In makeLazy\n";
24$reflector->resetAsLazyGhost($obj, function ($obj) {
25    var_dump("initializer");
26    $obj->__construct();
27});
28print "After makeLazy\n";
29
30var_dump($obj->a);
31$obj = null;
32
33print "# Proxy:\n";
34
35$obj = new C();
36print "In makeLazy\n";
37$reflector->resetAsLazyProxy($obj, function ($obj) {
38    var_dump("initializer");
39    return new C();
40});
41print "After makeLazy\n";
42
43var_dump($obj->a);
44$obj = null;
45
46?>
47--EXPECT--
48# Ghost:
49In makeLazy
50string(13) "C::__destruct"
51After makeLazy
52string(11) "initializer"
53int(1)
54string(13) "C::__destruct"
55# Proxy:
56In makeLazy
57string(13) "C::__destruct"
58After makeLazy
59string(11) "initializer"
60int(1)
61string(13) "C::__destruct"
62