1--TEST-- 2Lazy objects: clone is independent of the original object 3--FILE-- 4<?php 5 6class SomeObj { 7 public Value $value; 8 public function __construct() { 9 $this->value = new Value(); 10 } 11 public function __clone() { 12 $this->value = clone $this->value; 13 } 14} 15 16class Value { 17 public string $value = 'A'; 18} 19 20function test(string $name, object $obj) { 21 printf("# %s:\n", $name); 22 23 $reflector = new ReflectionClass(SomeObj::class); 24 25 $clonedObj = clone $obj; 26 var_dump($clonedObj->value->value); 27 28 $reflector->initializeLazyObject($obj); 29 $obj->value->value = 'B'; 30 31 $reflector->initializeLazyObject($clonedObj); 32 33 var_dump($obj->value->value); 34 var_dump($clonedObj->value->value); 35} 36 37$reflector = new ReflectionClass(SomeObj::class); 38 39test('Ghost', $reflector->newLazyGhost(function ($obj) { 40 $obj->__construct(); 41})); 42 43test('Proxy', $reflector->newLazyProxy(function () { 44 return new SomeObj(); 45})); 46 47?> 48--EXPECT-- 49# Ghost: 50string(1) "A" 51string(1) "B" 52string(1) "A" 53# Proxy: 54string(1) "A" 55string(1) "B" 56string(1) "A" 57