PHP: Hypertext Preprocessor (original) (raw)
ReflectionClass::newLazyProxy
(PHP 8 >= 8.4.0)
ReflectionClass::newLazyProxy — Creates a new lazy proxy instance
Description
public ReflectionClass::newLazyProxy(callable $factory
, int $options
= 0): object
Creates a new lazy proxy instance of the class, attaching thefactory
function to it. The constructor is not called, and properties are not set to their default values. When an attempt is made to observe or modify the proxy's state for the first time, the factory function is called to provide a real instance, which is then attached to the proxy. After this, all subsequent interactions with the proxy are forwarded to the real instance. SeeInitialization Triggers and Initialization Sequence.
Parameters
factory
The factory is a callback with the following signature:
object
The object
being initialized. At this point, the object is no longer marked as lazy, and accessing it does not trigger initialization again.
The factory function must return an object, referred to as the_real instance_, which is then attached to the proxy. This real instance must not be lazy and must not be the proxy itself. If the real instance does not have the same class as the proxy, the proxy's class must be a subclass of the real instance's class, without additional properties, and must not override the__destruct() or __clone() methods.
options
options
can be a combination of the following flags:
[ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE](class.reflectionclass.php#reflectionclass.constants.skip-initialization-on-serialize)
By default, serializing a lazy object triggers its initialization. Setting this flag prevents initialization, allowing lazy objects to be serialized without being initialized.
Return Values
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 alsoLifecycle of Lazy Objects.
Errors/Exceptions
An Error if the class is internal or extends an internal class except stdClass.
Examples
Example #1 Basic usage
<?php class Example { public function __construct(public int $prop) { echo __METHOD__, "\n"; } }$reflector = new ReflectionClass(Example::class); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>o</mi><mi>b</mi><mi>j</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">object = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">o</span><span class="mord mathnormal" style="margin-right:0.05724em;">bj</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>reflector->newLazyProxy(function (Example $object) { $realInstance = new Example(1); return $realInstance; });var_dump($object); var_dump($object instanceof Example);// Triggers initialization, and forwards the property fetch to the real instance var_dump($object->prop);var_dump($object); ?>
The above example will output:
lazy proxy object(Example)#3 (0) { ["prop"]=> uninitialized(int) } bool(true) Example::__construct int(1) lazy proxy object(Example)#3 (1) { ["instance"]=> object(Example)#4 (1) { ["prop"]=> int(1) } }
See Also
- Lazy objects
- ReflectionClass::newLazyGhost() - Creates a new lazy ghost instance
- ReflectionClass::newInstanceWithoutConstructor() - Creates a new class instance without invoking the constructor
- ReflectionClass::resetAsLazyProxy() - Resets an object and marks it as lazy
- ReflectionClass::markLazyObjectAsInitialized() - Marks a lazy object as initialized without calling the initializer or factory
- ReflectionClass::initializeLazyObject() - Forces initialization of a lazy object
- ReflectionClass::isUninitializedLazyObject() - Checks if an object is lazy and uninitialized
- ReflectionProperty::setRawValueWithoutLazyInitialization() - Set raw property value without triggering lazy initialization
- ReflectionProperty::skipLazyInitialization() - Marks property as non-lazy
- ReflectionProperty::isLazy() - Checks whether a property is lazy
Found A Problem?
There are no user contributed notes for this page.