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