1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionclass.newlazyproxy" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<refnamediv>
<refname>ReflectionClass::newLazyProxy</refname>
<refpurpose>Creates a new lazy proxy instance</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis role="ReflectionClass">
<modifier>public</modifier> <type>object</type><methodname>ReflectionClass::newLazyProxy</methodname>
<methodparam><type>callable</type><parameter>factory</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
<simpara>
Creates a new lazy proxy instance of the class, attaching the
<parameter>factory</parameter> 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. See
<link linkend="language.oop5.lazy-objects.initialization-triggers">Initialization
Triggers</link> and <link linkend="language.oop5.lazy-objects.initialization-sequence">
Initialization Sequence</link>.
</simpara>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>factory</parameter></term>
<listitem>
<simpara>
The factory is a callback with the following signature:
</simpara>
<para>
<methodsynopsis>
<type>object</type><methodname><replaceable>factory</replaceable></methodname>
<methodparam><type>object</type><parameter>object</parameter></methodparam>
</methodsynopsis>
<variablelist>
<varlistentry>
<term><parameter>object</parameter></term>
<listitem>
<simpara>
The <parameter>object</parameter> being initialized. At this point,
the object is no longer marked as lazy, and accessing it does not
trigger initialization again.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
<simpara>
The factory function must return an object, referred to as the
<emphasis>real instance</emphasis>, 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
<methodname>__destruct</methodname> or <methodname>__clone</methodname>
methods.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('reflectionclass.newlazyghost.parameters.options')/*)">
<xi:fallback/>
</xi:include>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<simpara>
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
<link linkend="language.oop5.lazy-objects.lifecycle">Lifecycle of Lazy
Objects</link>.
</simpara>
</refsect1>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('reflectionclass.newlazyghost')/db:refsect1[@role='errors'])">
<xi:fallback/>
</xi:include>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title>Basic usage</title>
<programlisting role="php">
<![CDATA[
<?php
class Example {
public function __construct(public int $prop) {
echo __METHOD__, "\n";
}
}
$reflector = new ReflectionClass(Example::class);
$object = $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);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
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)
}
}
]]>
</screen>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><link linkend="language.oop5.lazy-objects">Lazy objects</link></member>
<member><methodname>ReflectionClass::newLazyGhost</methodname></member>
<member><methodname>ReflectionClass::newInstanceWithoutConstructor</methodname></member>
<member><methodname>ReflectionClass::resetAsLazyProxy</methodname></member>
<member><methodname>ReflectionClass::markLazyObjectAsInitialized</methodname></member>
<member><methodname>ReflectionClass::initializeLazyObject</methodname></member>
<member><methodname>ReflectionClass::isUninitializedLazyObject</methodname></member>
<member><methodname>ReflectionProperty::setRawValueWithoutLazyInitialization</methodname></member>
<member><methodname>ReflectionProperty::skipLazyInitialization</methodname></member>
<member><methodname>ReflectionProperty::isLazy</methodname></member>
</simplelist>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|