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