(PHP 8 >= 8.4.0)
ReflectionClass::newLazyGhost — Creates a new lazy ghost instance
Creates a new lazy ghost instance of the class, attaching the
initializer
to it. The constructor is not called, and
properties are not set to their default value. However, the object will
be automatically initialized by invoking the
initializer
the first time its state is observed or
modified. See
Initialization
Triggers and
Initialization Sequence.
initializer
object
object
being initialized. At this point,
the object is no longer marked as lazy, and accessing it does not
trigger initialization again.
initializer
function must return null
or no
value.
options
options
can be a combination of the following
flags:
ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE
Returns a lazy proxy instance. If the object has no properties, or if all its properties are static or virtual, a normal (non-lazy) instance is returned. See also Lifecycle of Lazy Objects).
Пример #1 Basic usage
<?php
class Example {
public function __construct(public int $prop) {
echo __METHOD__, "\n";
}
}
$reflector = new ReflectionClass(Example::class);
$object = $reflector->newLazyGhost(function (Example $object) {
$object->__construct(1);
});
var_dump($object);
var_dump($object instanceof Example);
// Triggers initialization, and fetches the property after that
var_dump($object->prop);
?>
Результат выполнения приведённого примера:
lazy ghost object(Example)#3 (0) { ["prop"]=> uninitialized(int) } bool(true) Example::__construct int(1)