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 179 180 181 182 183 184 185
|
<?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
-->
|