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