xref: /PHP-8.4/Zend/tests/gc_049.phpt (revision a470110f)
1--TEST--
2GC 049: Objects created during GC do not participate in the same collection
3--FILE--
4<?php
5
6class CycleWithDestructor
7{
8    private \Closure $destructorFx;
9
10    private \stdClass $cycleRef;
11
12    public function __construct(\Closure $destructorFx)
13    {
14        $this->destructorFx = $destructorFx;
15        $this->cycleRef = new \stdClass();
16        $this->cycleRef->x = $this;
17    }
18
19    public function __destruct()
20    {
21        ($this->destructorFx)();
22    }
23}
24
25$isSecondGcRerun = false; // https://github.com/php/php-src/commit/b58d74547f
26$createFx = static function () use (&$createFx, &$isSecondGcRerun): void {
27    $destructorFx = static function () use (&$createFx, &$isSecondGcRerun): void {
28        if (!gc_status()['running']) {
29            echo "gc shutdown\n";
30            return;
31        }
32
33        echo "gc" . ($isSecondGcRerun ? ' rerun' : '') . "\n";
34
35        $isSecondGcRerun = !$isSecondGcRerun;
36
37        $createFx();
38    };
39
40    new CycleWithDestructor($destructorFx);
41};
42
43$createFx();
44gc_collect_cycles();
45gc_collect_cycles();
46gc_collect_cycles();
47echo "---\n";
48gc_collect_cycles();
49gc_collect_cycles();
50gc_collect_cycles();
51echo "---\n";
52?>
53--EXPECT--
54gc
55gc rerun
56gc
57gc rerun
58gc
59gc rerun
60---
61gc
62gc rerun
63gc
64gc rerun
65gc
66gc rerun
67---
68gc shutdown
69