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