<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.oop5.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.oop5.lazy-objects.php',
    1 => 'Lazy Objects',
    2 => 'Lazy Objects',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Класи та об\'єкти',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.variance.php',
    1 => 'Covariance and Contravariance',
  ),
  'next' => 
  array (
    0 => 'language.oop5.changelog.php',
    1 => 'OOP Changelog',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/oop5/lazy-objects.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.lazy-objects" class="sect1">
 <h2 class="title">Lazy Objects</h2>

 <p class="simpara">
  A lazy object is an object whose initialization is deferred until its
  state is observed or modified. Some use-case examples include dependency
  injection components that provide lazy services fully initialized only if
  needed, <abbr>ORM</abbr>s providing lazy entities that hydrate
  themselves from the database only when accessed, or a JSON parser that
  delays parsing until elements are accessed.
 </p>

 <p class="simpara">
  Two lazy object strategies are supported: Ghost Objects and Virtual
  Proxies, hereafter referred to as &quot;lazy ghosts&quot; and
  &quot;lazy proxies&quot;.
  In both strategies, the lazy object is attached to an initializer or factory
  that is called automatically when its state is observed or modified for
  the first time.  From an abstraction point of view, lazy ghost objects are
  indistinguishable from non-lazy ones: they can be used without knowing they
  are lazy, allowing them to be passed to and used by code that is unaware of
  laziness. Lazy proxies are similarly transparent, but care must be taken when
  their identity is used, as the proxy and its real instance have different
  identities.
 </p>

 <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
  <strong>Version Information</strong><br />
  <span class="simpara">
   Lazy objects were introduced in PHP 8.4.
  </span>
 </p></blockquote>

 <div class="sect2" id="language.oop5.lazy-objects.creation">
  <h3 class="title">Creating Lazy Objects</h3>

  <p class="simpara">
   It is possible to create a lazy instance of any user defined class or the
   <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> class (other internal classes are not
   supported), or to reset an instance of these classes to make it lazy.
   The entry points for creating a lazy object are the
   <span class="methodname"><a href="reflectionclass.newlazyghost.php" class="methodname">ReflectionClass::newLazyGhost()</a></span> and
   <span class="methodname"><a href="reflectionclass.newlazyproxy.php" class="methodname">ReflectionClass::newLazyProxy()</a></span> methods.
  </p>

  <p class="simpara">
   Both methods accept a function that is called when the object requires
   initialization. The function&#039;s expected behavior varies depending on the
   strategy in use, as described in the reference documentation for each method.
  </p>

  <div class="example" id="example-1">
   <p><strong>Приклад #1 Creating a Lazy Ghost</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Example<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(public </span><span style="color: #0000BB">int $prop</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$reflector </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #0000BB">Example</span><span style="color: #007700">::class);<br /></span><span style="color: #0000BB">$lazyObject </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">newLazyGhost</span><span style="color: #007700">(function (</span><span style="color: #0000BB">Example $object</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// Initialize object in-place<br />    </span><span style="color: #0000BB">$object</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />});<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$lazyObject</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$lazyObject</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Triggers initialization<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$lazyObject</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prop</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
lazy ghost object(Example)#3 (0) {
[&quot;prop&quot;]=&gt;
uninitialized(int)
}
string(7) &quot;Example&quot;
Example::__construct
int(1)
</pre></div>
   </div>
  </div>

  <div class="example" id="example-2">
   <p><strong>Приклад #2 Creating a Lazy Proxy</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Example<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(public </span><span style="color: #0000BB">int $prop</span><span style="color: #007700">)<br />    {<br />        echo </span><span style="color: #0000BB">__METHOD__</span><span style="color: #007700">, </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$reflector </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #0000BB">Example</span><span style="color: #007700">::class);<br /></span><span style="color: #0000BB">$lazyObject </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">newLazyProxy</span><span style="color: #007700">(function (</span><span style="color: #0000BB">Example $object</span><span style="color: #007700">) {<br />    </span><span style="color: #FF8000">// Create and return the real instance<br />    </span><span style="color: #007700">return new </span><span style="color: #0000BB">Example</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />});<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$lazyObject</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">get_class</span><span style="color: #007700">(</span><span style="color: #0000BB">$lazyObject</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Triggers initialization<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$lazyObject</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prop</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
lazy proxy object(Example)#3 (0) {
  [&quot;prop&quot;]=&gt;
  uninitialized(int)
}
string(7) &quot;Example&quot;
Example::__construct
int(1)
</pre></div>
   </div>
  </div>

  <p class="simpara">
   Any access to properties of a lazy object triggers its initialization
   (including via <span class="classname"><a href="class.reflectionproperty.php" class="classname">ReflectionProperty</a></span>).
   However, certain properties might be known in advance and should not trigger
   initialization when accessed:
  </p>

  <div class="example" id="example-3">
   <p><strong>Приклад #3 Initializing Properties Eagerly</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">BlogPost<br /></span><span style="color: #007700">{<br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(<br />        public </span><span style="color: #0000BB">int $id</span><span style="color: #007700">,<br />        public </span><span style="color: #0000BB">string $title</span><span style="color: #007700">,<br />        public </span><span style="color: #0000BB">string $content</span><span style="color: #007700">,<br />    ) { }<br />}<br /><br /></span><span style="color: #0000BB">$reflector </span><span style="color: #007700">= new </span><span style="color: #0000BB">ReflectionClass</span><span style="color: #007700">(</span><span style="color: #0000BB">BlogPost</span><span style="color: #007700">::class);<br /><br /></span><span style="color: #0000BB">$post </span><span style="color: #007700">= </span><span style="color: #0000BB">$reflector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">newLazyGhost</span><span style="color: #007700">(function (</span><span style="color: #0000BB">$post</span><span style="color: #007700">) {<br />    </span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">fetch_from_store</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">);<br />    </span><span style="color: #0000BB">$post</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">], </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'title'</span><span style="color: #007700">], </span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #DD0000">'content'</span><span style="color: #007700">]);<br />});<br /><br /></span><span style="color: #FF8000">// Without this line, the following call to ReflectionProperty::setValue() would<br />// trigger initialization.<br /></span><span style="color: #0000BB">$reflector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'id'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">skipLazyInitialization</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$reflector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'id'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">setValue</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">, </span><span style="color: #0000BB">123</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Alternatively, one can use this directly:<br /></span><span style="color: #0000BB">$reflector</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'id'</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">setRawValueWithoutLazyInitialization</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">, </span><span style="color: #0000BB">123</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// The id property can be accessed without triggering initialization<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$post</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">id</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <p class="simpara">
   The <span class="methodname"><a href="reflectionproperty.skiplazyinitialization.php" class="methodname">ReflectionProperty::skipLazyInitialization()</a></span> and
   <span class="methodname"><a href="reflectionproperty.setrawvaluewithoutlazyinitialization.php" class="methodname">ReflectionProperty::setRawValueWithoutLazyInitialization()</a></span>
   methods offer ways to bypass lazy-initialization when accessing a property.
  </p>
 </div>

 <div class="sect2" id="language.oop5.lazy-objects.patterns">
  <h3 class="title">About Lazy Object Strategies</h3>

  <p class="simpara">
   <em>Lazy ghosts</em> are objects that initialize in-place and,
   once initialized, are indistinguishable from an object that was never lazy.
   This strategy is suitable when we control both the instantiation and
   initialization of the object, making it unsuitable if either of these is
   managed by another party.
  </p>

  <p class="simpara">
   <em>Lazy proxies</em>, once initialized, act as proxies to
   a real instance: any operation on an initialized lazy proxy is forwarded
   to the real instance. The creation of the real instance can be delegated
   to another party, making this strategy useful in cases where lazy ghosts
   are unsuitable. Although lazy proxies are nearly as transparent as lazy
   ghosts, caution is needed when their identity is used, as the proxy and
   its real instance have distinct identities.
  </p>
 </div>

 <div class="sect2" id="language.oop5.lazy-objects.lifecycle">
  <h3 class="title">Lifecycle of Lazy Objects</h3>

  <p class="simpara">
   Objects can be made lazy at instantiation time using
   <span class="methodname"><a href="reflectionclass.newlazyghost.php" class="methodname">ReflectionClass::newLazyGhost()</a></span> or
   <span class="methodname"><a href="reflectionclass.newlazyproxy.php" class="methodname">ReflectionClass::newLazyProxy()</a></span>, or after
   instantiation by using
   <span class="methodname"><a href="reflectionclass.resetaslazyghost.php" class="methodname">ReflectionClass::resetAsLazyGhost()</a></span> or
   <span class="methodname"><a href="reflectionclass.resetaslazyproxy.php" class="methodname">ReflectionClass::resetAsLazyProxy()</a></span>. Following this, a
   lazy object can become initialized through one of the following operations:
  </p>

  <ul class="simplelist">
   <li>
    Interacting with the object in a way that triggers automatic initialization. See
    <a href="language.oop5.lazy-objects.php#language.oop5.lazy-objects.initialization-triggers" class="link">Initialization
    triggers</a>.
   </li>
   <li>
    Marking all its properties as non-lazy using
    <span class="methodname"><a href="reflectionproperty.skiplazyinitialization.php" class="methodname">ReflectionProperty::skipLazyInitialization()</a></span> or
    <span class="methodname"><a href="reflectionproperty.setrawvaluewithoutlazyinitialization.php" class="methodname">ReflectionProperty::setRawValueWithoutLazyInitialization()</a></span>.
   </li>
   <li>
    Calling explicitly <span class="methodname"><a href="reflectionclass.initializelazyobject.php" class="methodname">ReflectionClass::initializeLazyObject()</a></span>
    or <span class="methodname"><a href="reflectionclass.marklazyobjectasinitialized.php" class="methodname">ReflectionClass::markLazyObjectAsInitialized()</a></span>.
   </li>
  </ul>

  <p class="simpara">
   As lazy objects become initialized when all their properties are marked
   non-lazy, the above methods will not mark an object as lazy if no properties
   could be marked as lazy.
  </p>
 </div>

 <div class="sect2" id="language.oop5.lazy-objects.initialization-triggers">
  <h3 class="title">Initialization Triggers</h3>

  <p class="simpara">
   Lazy objects are designed to be fully transparent to their consumers,
   so normal operations that observe or modify the object&#039;s state will
   automatically trigger initialization before the operation is performed. This
   includes, but is not limited to, the following operations:
  </p>

  <ul class="simplelist">
   <li>
    Reading or writing a property.
   </li>
   <li>
    Testing if a property is set or unsetting it.
   </li>
   <li>
    Accessing or modifying a property via
    <span class="methodname"><a href="reflectionproperty.getvalue.php" class="methodname">ReflectionProperty::getValue()</a></span>,
    <span class="methodname"><a href="reflectionproperty.getrawvalue.php" class="methodname">ReflectionProperty::getRawValue()</a></span>,
    <span class="methodname"><a href="reflectionproperty.setvalue.php" class="methodname">ReflectionProperty::setValue()</a></span>,
    or <span class="methodname"><a href="reflectionproperty.setrawvalue.php" class="methodname">ReflectionProperty::setRawValue()</a></span>.
   </li>
   <li>
    Listing properties with
    <span class="methodname"><strong>ReflectionObject::getProperties()</strong></span>,
    <span class="methodname"><strong>ReflectionObject::getProperty()</strong></span>,
    <span class="function"><a href="function.get-object-vars.php" class="function">get_object_vars()</a></span>.
   </li>
   <li>
    Iterating over properties of an object that does not implement
    <span class="interfacename"><a href="class.iterator.php" class="interfacename">Iterator</a></span> or
    <span class="interfacename"><a href="class.iteratoraggregate.php" class="interfacename">IteratorAggregate</a></span> using
    <a href="control-structures.foreach.php" class="link">foreach</a>.
   </li>
   <li>
    Serializing the object with <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span>,
    <span class="function"><a href="function.json-encode.php" class="function">json_encode()</a></span>, etc.
   </li>
   <li>
    <a href="language.oop5.lazy-objects.php#language.oop5.lazy-objects.cloning" class="link">Cloning</a> the
    object.
   </li>
  </ul>

  <p class="simpara">
   Method calls that do not access the object state will not trigger
   initialization. Similarly, interactions with the object that invoke magic
   methods or hook functions will not trigger initialization if these methods
   or functions do not access the object&#039;s state.
  </p>

  <div class="sect3">
   <h4 class="title">Non-Triggering Operations</h4>

   <p class="simpara">
    The following specific methods or low-level operations allow access or
    modification of lazy objects without triggering initialization:
   </p>

   <ul class="simplelist">
    <li>
     Marking properties as non-lazy with
     <span class="methodname"><a href="reflectionproperty.skiplazyinitialization.php" class="methodname">ReflectionProperty::skipLazyInitialization()</a></span> or
     <span class="methodname"><a href="reflectionproperty.setrawvaluewithoutlazyinitialization.php" class="methodname">ReflectionProperty::setRawValueWithoutLazyInitialization()</a></span>.
    </li>
    <li>
     Retrieving the internal representation of properties using
     <span class="function"><a href="function.get-mangled-object-vars.php" class="function">get_mangled_object_vars()</a></span> or by
     <a href="language.types.array.php#language.types.array.casting" class="link">casting the object to an
     array</a>.
    </li>
    <li>
     Using <span class="function"><a href="function.serialize.php" class="function">serialize()</a></span> when
     <strong><code><a href="class.reflectionclass.php#reflectionclass.constants.skip-initialization-on-serialize">ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE</a></code></strong>
     is set, unless
     <a href="language.oop5.magic.php#object.serialize" class="link">__serialize()</a> or
     <a href="language.oop5.magic.php#object.sleep" class="link">__sleep()</a> trigger initialization.
    </li>
    <li>
     Calling to <span class="methodname"><strong>ReflectionObject::__toString()</strong></span>.
    </li>
    <li>
     Using <span class="function"><a href="function.var-dump.php" class="function">var_dump()</a></span> or
     <span class="function"><a href="function.debug-zval-dump.php" class="function">debug_zval_dump()</a></span>, unless
     <a href="language.oop5.magic.php#object.debuginfo" class="link">__debugInfo()</a> triggers
     initialization.
    </li>
   </ul>
  </div>
 </div>

 <div class="sect2" id="language.oop5.lazy-objects.initialization-sequence">
  <h3 class="title">Initialization Sequence</h3>

  <p class="simpara">
   This section outlines the sequence of operations performed when
   initialization is triggered, based on the strategy in use.
  </p>

  <div class="sect3">
   <h4 class="title">Ghost Objects</h4>
   <ul class="simplelist">
    <li>
     The object is marked as non-lazy.
    </li>
    <li>
     Properties not initialized with
     <span class="methodname"><a href="reflectionproperty.skiplazyinitialization.php" class="methodname">ReflectionProperty::skipLazyInitialization()</a></span> or
     <span class="methodname"><a href="reflectionproperty.setrawvaluewithoutlazyinitialization.php" class="methodname">ReflectionProperty::setRawValueWithoutLazyInitialization()</a></span>
     are set to their default values, if any. At this stage, the object
     resembles one created with
     <span class="methodname"><a href="reflectionclass.newinstancewithoutconstructor.php" class="methodname">ReflectionClass::newInstanceWithoutConstructor()</a></span>,
     except for already initialized properties.
    </li>
    <li>
     The initializer function is then called with the object as its first
     parameter. The function is expected, but not required, to initialize
     the object state, and must return <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> or no value. The object is no
     longer lazy at this point, so the function can access its properties
     directly.
    </li>
   </ul>
   <p class="simpara">
    After initialization, the object is indistinguishable from an object that
    was never lazy.
   </p>
  </div>

  <div class="sect3">
   <h4 class="title">Proxy Objects</h4>
   <ul class="simplelist">
    <li>
     The object is marked as non-lazy.
    </li>
    <li>
     Unlike ghost objects, the properties of the object are not modified at
     this stage.
    </li>
    <li>
     The factory function is called with the object as its first parameter and
     must return a non-lazy instance of a compatible class (see
     <span class="methodname"><a href="reflectionclass.newlazyproxy.php" class="methodname">ReflectionClass::newLazyProxy()</a></span>).
    </li>
    <li>
     The returned instance is referred to as the <em>real
     instance</em> and is attached to the proxy.
    </li>
    <li>
     The proxy's property values are discarded as though
     <span class="function"><a href="function.unset.php" class="function">unset()</a></span> was called.
    </li>
   </ul>
   <p class="simpara">
    After initialization, accessing any property on the proxy will
    yield the same result as accessing the corresponding property on
    the real instance; all property accesses on the proxy are forwarded
    to the real instance, including declared, dynamic, non-existing, or
    properties marked with
    <span class="methodname"><a href="reflectionproperty.skiplazyinitialization.php" class="methodname">ReflectionProperty::skipLazyInitialization()</a></span> or
    <span class="methodname"><a href="reflectionproperty.setrawvaluewithoutlazyinitialization.php" class="methodname">ReflectionProperty::setRawValueWithoutLazyInitialization()</a></span>.
   </p>
   <p class="simpara">
    The proxy object itself is <em>not</em> replaced or substituted
    for the real instance.
   </p>
   <p class="simpara">
    While the factory receives the proxy as its first parameter, it is
    not expected to modify it (modifications are allowed but will be lost
    during the final initialization step). However, the proxy can be used
    for decisions based on the values of initialized properties, the class,
    the object itself, or its identity. For instance, the initializer might
    use an initialized property&#039;s value when creating the real instance.
   </p>
  </div>

  <div class="sect3">
   <h4 class="title">Common Behavior</h4>

   <p class="simpara">
    The scope and <var class="varname">$this</var> context of the initializer or factory
    function remains unchanged, and usual visibility constraints apply.
   </p>

   <p class="simpara">
    After successful initialization, the initializer or factory function
    is no longer referenced by the object and may be released if it has no
    other references.
   </p>

   <p class="simpara">
    If the initializer throws an exception, the object state is reverted to its
    pre-initialization state and the object is marked as lazy again. In other
    words, all effects on the object itself are reverted. Other side effects,
    such as effects on other objects, are not reverted. This prevents
    exposing a partially initialized instance in case of failure.
   </p>
  </div>
 </div>

 <div class="sect2" id="language.oop5.lazy-objects.cloning">
  <h3 class="title">Cloning</h3>

  <p class="simpara">
   <a href="language.oop5.cloning.php" class="link">Cloning</a>
   a lazy object triggers its initialization before the clone is
   created, resulting in an initialized object.
  </p>

  <p class="simpara">
   For proxy objects, both the proxy and its real instance are cloned, and
   the clone of the proxy is returned.
   The <a href="language.oop5.cloning.php#object.clone" class="link"><code class="literal">__clone</code></a> method
   is called on the real instance, not on the proxy.
   The cloned proxy and real instance are linked as they are during
   initialization, so accesses to the proxy clone are forwarded to the real
   instance clone.
  </p>

  <p class="simpara">
   This behavior ensures that the clone and the original object maintain
   separate states. Changes to the original object or its initializer&#039;s state
   after cloning do not affect the clone. Cloning both the proxy and its real
   instance, rather than returning a clone of the real instance alone, ensures
   that the clone operation consistently returns an object of the same class.
  </p>
 </div>

 <div class="sect2" id="language.oop5.lazy-objects.destructors">
  <h3 class="title">Destructors</h3>

  <p class="simpara">
   For lazy ghosts, the destructor is only called if the object has been
   initialized. For proxies, the destructor is only called on the real instance,
   if one exists.
  </p>

  <p class="simpara">
   The <span class="methodname"><a href="reflectionclass.resetaslazyghost.php" class="methodname">ReflectionClass::resetAsLazyGhost()</a></span> and
   <span class="methodname"><a href="reflectionclass.resetaslazyproxy.php" class="methodname">ReflectionClass::resetAsLazyProxy()</a></span> methods may invoke
   the destructor of the object being reset.
  </p>
 </div>
</div><?php manual_footer($setup); ?>