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