1--TEST-- 2Lazy objects: serialize() initializes object if __serialize observes object state 3--FILE-- 4<?php 5 6class C { 7 public int $a; 8 public function __serialize() { 9 return ['a' => $this->a]; 10 } 11} 12 13function test(string $name, object $obj) { 14 printf("# %s:\n", $name); 15 16 $serialized = serialize($obj); 17 $unserialized = unserialize($serialized); 18 var_dump($serialized, $unserialized); 19} 20 21$reflector = new ReflectionClass(C::class); 22 23$obj = $reflector->newLazyGhost(function ($obj) { 24 var_dump("initializer"); 25 $obj->a = 1; 26}); 27 28test('Ghost', $obj); 29 30$obj = $reflector->newLazyProxy(function ($obj) { 31 var_dump("initializer"); 32 $c = new c(); 33 $c->a = 1; 34 return $c; 35}); 36 37test('Proxy', $obj); 38 39--EXPECTF-- 40# Ghost: 41string(11) "initializer" 42string(24) "O:1:"C":1:{s:1:"a";i:1;}" 43object(C)#%d (1) { 44 ["a"]=> 45 int(1) 46} 47# Proxy: 48string(11) "initializer" 49string(24) "O:1:"C":1:{s:1:"a";i:1;}" 50object(C)#%d (1) { 51 ["a"]=> 52 int(1) 53} 54