(PHP 8 >= 8.4.0)
ReflectionClass::markLazyObjectAsInitialized — Marks a lazy object as initialized without calling the initializer or factory
Marks a lazy object as initialized without calling the initializer or
factory. This has no effect if object
is not lazy or
is already initialized.
The effect of calling this method is the same as described for Ghost Objects
(regardless of the laziness strategy of object
) in
initialization
sequence, except that the initializer is not called.
After that, the object is indistinguishable from an object that was never
lazy and was created with
ReflectionClass::newInstanceWithoutConstructor(),
except for the value of properties that were already initialized with
ReflectionProperty::setRawValueWithoutLazyInitialization()
or ReflectionProperty::skipLazyInitialization().
object
Returns object
.
Приклад #1 Marking an uninitialized lazy object as initialized
<?php
class Example
{
public string $prop1;
public string $prop2;
public string $prop3 = 'default value';
}
$reflector = new ReflectionClass(Example::class);
$object = $reflector->newLazyGhost(function ($object) {
echo "Initializer called\n";
$object->prop1 = 'initialized';
});
$reflector->getProperty('prop1')
->setRawValueWithoutLazyInitialization($object, 'prop1 value');
var_dump($object);
$reflector->markLazyObjectAsInitialized($object);
var_dump($object);
?>
Поданий вище приклад виведе:
lazy ghost object(Example)#3 (1) { ["prop1"]=> string(11) "prop1 value" ["prop2"]=> uninitialized(string) ["prop3"]=> uninitialized(string) } object(Example)#3 (2) { ["prop1"]=> string(11) "prop1 value" ["prop2"]=> uninitialized(string) ["prop3"]=> string(13) "default value" }
Приклад #2 Marking an initialized object as initialized
<?php
class Example
{
public string $prop1;
public string $prop2;
public string $prop3 = 'default value';
}
$reflector = new ReflectionClass(Example::class);
$object = $reflector->newLazyGhost(function ($object) {
echo "Initializer called\n";
$object->prop1 = 'initialized';
});
$reflector->getProperty('prop1')
->setRawValueWithoutLazyInitialization($object, 'prop1 value');
var_dump($object->prop3);
var_dump($object);
$reflector->markLazyObjectAsInitialized($object);
var_dump($object);
?>
Поданий вище приклад виведе:
Initializer called string(13) "default value" object(Example)#3 (2) { ["prop1"]=> string(11) "initialized" ["prop2"]=> uninitialized(string) ["prop3"]=> string(13) "default value" } object(Example)#3 (2) { ["prop1"]=> string(11) "initialized" ["prop2"]=> uninitialized(string) ["prop3"]=> string(13) "default value" }