1--TEST-- 2Lazy objects: RFC example 001 3--FILE-- 4<?php 5 6class MyClass 7{ 8 public function __construct(private int $foo) 9 { 10 // Heavy initialization logic here. 11 } 12 13 public function getFoo() 14 { 15 return $this->foo; 16 } 17} 18 19$initializer = static function (MyClass $ghost): void { 20 $ghost->__construct(123); 21}; 22 23$reflector = new ReflectionClass(MyClass::class); 24$object = $reflector->newLazyGhost($initializer); 25 26var_dump($object); 27var_dump($object->getFoo()); 28var_dump($object); 29 30?> 31--EXPECTF-- 32lazy ghost object(MyClass)#%d (0) { 33 ["foo":"MyClass":private]=> 34 uninitialized(int) 35} 36int(123) 37object(MyClass)#%d (1) { 38 ["foo":"MyClass":private]=> 39 int(123) 40} 41