ReflectionClass::markLazyObjectAsInitialized

(PHP 8 >= 8.4.0)

ReflectionClass::markLazyObjectAsInitializedMarks a lazy object as initialized without calling the initializer or factory

説明

public ReflectionClass::markLazyObjectAsInitialized(object $object): object

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
The object to mark as initialized.

戻り値

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"
}

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top