1--TEST-- 2Lazy objects: 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->a; 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" 47int(1) 48object(C)#%d (1) { 49 ["a"]=> 50 int(1) 51} 52# Proxy: 53lazy proxy object(C)#%d (0) { 54 ["a"]=> 55 uninitialized(int) 56} 57string(11) "initializer" 58string(14) "C::__construct" 59int(1) 60lazy proxy object(C)#%d (1) { 61 ["instance"]=> 62 object(C)#%d (1) { 63 ["a"]=> 64 int(1) 65 } 66} 67