1
0
mirror of https://github.com/php/doc-en.git synced 2026-04-29 10:13:23 +02:00
Files
2024-11-09 14:35:59 +00:00

186 lines
5.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<refentry xml:id="reflectionclass.newlazyghost" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>ReflectionClass::newLazyGhost</refname>
<refpurpose>Creates a new lazy ghost instance</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis role="ReflectionClass">
<modifier>public</modifier> <type>object</type> <methodname>ReflectionClass::newLazyGhost</methodname>
<methodparam><type>callable</type><parameter>initializer</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
<simpara>
Creates a new lazy ghost instance of the class, attaching the
<parameter>initializer</parameter> to it. The constructor is not called, and
properties are not set to their default value. However, the object will
be automatically initialized by invoking the
<parameter>initializer</parameter> the first time its state is observed or
modified. 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>initializer</parameter></term>
<listitem>
<simpara>
The initializer is a callback with the following signature:
</simpara>
<para>
<methodsynopsis>
<type>void</type><methodname><replaceable>initializer</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 <parameter>initializer</parameter> function must return &null; or no
value.
</simpara>
</listitem>
</varlistentry>
<varlistentry xml:id="reflectionclass.newlazyghost.parameters.options">
<term><parameter>options</parameter></term>
<listitem>
<para>
<parameter>options</parameter> can be a combination of the following
flags:
<variablelist>
<varlistentry>
<term>
<constant>ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE</constant>
</term>
<listitem>
<simpara>
By default, serializing a lazy object triggers its
initialization. Setting this flag prevents initialization, allowing
lazy objects to be serialized without being initialized.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<simpara>
Returns a lazy ghost 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>
<refsect1 role="errors">
&reftitle.errors;
<simpara>
An <classname>Error</classname> if the class is internal or extends an
internal class except <classname>stdClass</classname>.
</simpara>
</refsect1>
<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->newLazyGhost(function (Example $object) {
$object->__construct(1);
});
var_dump($object);
var_dump($object instanceof Example);
// Triggers initialization, and fetches the property after that
var_dump($object->prop);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
lazy ghost object(Example)#3 (0) {
["prop"]=>
uninitialized(int)
}
bool(true)
Example::__construct
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::newLazyProxy</methodname></member>
<member><methodname>ReflectionClass::newInstanceWithoutConstructor</methodname></member>
<member><methodname>ReflectionClass::resetAsLazyGhost</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
-->