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