1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-24 07:42:10 +01:00
Files
2024-11-09 14:35:59 +00:00

179 lines
6.0 KiB
XML

<?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
-->