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