1--TEST-- 2Lazy objects: GC 004 3--FILE-- 4<?php 5 6class Canary { 7 public $value; 8 public function __destruct() { 9 var_dump(__FUNCTION__); 10 } 11} 12 13class C { 14} 15 16function ghost() { 17 printf("# Ghost:\n"); 18 19 $canary = new Canary(); 20 21 $obj = (new ReflectionClass(C::class))->newInstanceWithoutConstructor(); 22 (new ReflectionClass($obj))->resetAsLazyGhost($obj, function () use ($canary) { 23 }); 24 25 var_dump($obj); // initializes property hash 26 27 $canary->value = $obj; 28 $obj = null; 29 $canary = null; 30 31 gc_collect_cycles(); 32} 33 34function proxy() { 35 printf("# Proxy:\n"); 36 37 $canary = new Canary(); 38 39 $obj = (new ReflectionClass(C::class))->newInstanceWithoutConstructor(); 40 (new ReflectionClass($obj))->resetAsLazyProxy($obj, function () use ($canary) { 41 return new C(); 42 }); 43 44 var_dump($obj); // initializes property hash 45 46 $canary->value = $obj; 47 $obj = null; 48 $canary = null; 49 50 gc_collect_cycles(); 51} 52 53ghost(); 54proxy(); 55 56?> 57==DONE== 58--EXPECTF-- 59# Ghost: 60object(C)#%d (0) { 61} 62string(10) "__destruct" 63# Proxy: 64object(C)#%d (0) { 65} 66string(10) "__destruct" 67==DONE== 68