<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.oop5.cloning.php',
    1 => 'Object Cloning',
    2 => 'Object Cloning',
  ),
  'up' => 
  array (
    0 => 'language.oop5.php',
    1 => 'Classes and Objects',
  ),
  'prev' => 
  array (
    0 => 'language.oop5.final.php',
    1 => 'Final Keyword',
  ),
  'next' => 
  array (
    0 => 'language.oop5.object-comparison.php',
    1 => 'Comparing Objects',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/oop5/cloning.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.oop5.cloning" class="sect1">
  <h2 class="title">Object Cloning</h2>
  
  <p class="para">
   Creating a copy of an object with fully replicated properties is not
   always the wanted behavior. A good example of the need for copy
   constructors, is if you have an object which represents a GTK window and the
   object holds the resource of this GTK window, when you create a duplicate
   you might want to create a new window with the same properties and have the
   new object hold the resource of the new window. Another example is if your
   object holds a reference to another object which it uses and when you
   replicate the parent object you want to create a new instance of this other
   object so that the replica has its own separate copy.
  </p>

  <p class="para">
   An object copy is created by using the <code class="literal">clone</code> keyword
   (which calls the object&#039;s <a href="language.oop5.cloning.php#object.clone" class="link">__clone()</a>
   method if possible).
  </p>

  <div class="informalexample">
   <div class="example-contents">
<div class="annotation-interactive cdata"><pre>
$copy_of_object = clone $object;
</pre></div>
   </div>

  </div>

  <p class="para">
   When an object is cloned, PHP will perform a shallow copy of all of the
   object&#039;s properties. Any properties that are references to other variables
   will remain references.
  </p>

  <div class="methodsynopsis dc-description" id="object.clone">
   <span class="methodname"><strong>__clone</strong></span>(): <span class="type"><a href="language.types.void.php" class="type void">void</a></span></div>


  <p class="para">
   Once the cloning is complete, if a <a href="language.oop5.cloning.php#object.clone" class="link">__clone()</a> method is defined, then
   the newly created object&#039;s <a href="language.oop5.cloning.php#object.clone" class="link">__clone()</a> method will be called, to allow any
   necessary properties that need to be changed.
  </p>

  <div class="example" id="example-1">
   <p><strong>Example #1 Cloning an object</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">SubObject<br /></span><span style="color: #007700">{<br />    static </span><span style="color: #0000BB">$instances </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">$instance</span><span style="color: #007700">;<br /><br />    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">() {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">instance </span><span style="color: #007700">= ++</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$instances</span><span style="color: #007700">;<br />    }<br /><br />    public function </span><span style="color: #0000BB">__clone</span><span style="color: #007700">() {<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">instance </span><span style="color: #007700">= ++</span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">$instances</span><span style="color: #007700">;<br />    }<br />}<br /><br />class </span><span style="color: #0000BB">MyCloneable<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$object1</span><span style="color: #007700">;<br />    public </span><span style="color: #0000BB">$object2</span><span style="color: #007700">;<br /><br />    function </span><span style="color: #0000BB">__clone</span><span style="color: #007700">()<br />    {<br />        </span><span style="color: #FF8000">// Force a copy of this-&gt;object, otherwise<br />        // it will point to same object.<br />        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">object1 </span><span style="color: #007700">= clone </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">object1</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyCloneable</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">object1 </span><span style="color: #007700">= new </span><span style="color: #0000BB">SubObject</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">object2 </span><span style="color: #007700">= new </span><span style="color: #0000BB">SubObject</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">$obj2 </span><span style="color: #007700">= clone </span><span style="color: #0000BB">$obj</span><span style="color: #007700">;<br /><br /><br />print </span><span style="color: #DD0000">"Original Object:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj</span><span style="color: #007700">);<br /><br />print </span><span style="color: #DD0000">"Cloned Object:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$obj2</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
Original Object:
MyCloneable Object
(
    [object1] =&gt; SubObject Object
        (
            [instance] =&gt; 1
        )

    [object2] =&gt; SubObject Object
        (
            [instance] =&gt; 2
        )

)
Cloned Object:
MyCloneable Object
(
    [object1] =&gt; SubObject Object
        (
            [instance] =&gt; 3
        )

    [object2] =&gt; SubObject Object
        (
            [instance] =&gt; 2
        )

)
</pre></div>

   </div>

  </div>

  <p class="para">
   It is possible to access a member of a freshly cloned
   object in a single expression:
  </p>
  <div class="example" id="example-2">
   <p><strong>Example #2 Access member of freshly cloned object</strong></p>
   <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$dateTime </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">();<br />echo (clone </span><span style="color: #0000BB">$dateTime</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output
something similar to:</p></div>
   <div class="example-contents screen">
<div class="annotation-interactive cdata"><pre>
2016
</pre></div>
   </div>
  </div>

 </div><?php manual_footer($setup); ?>