Cloning a PHP object is not working
<p>I'm having the same poblem <a href="https://stackoverflow.com/questions/4123571/how-to-make-a-copy-of-an-object-without-reference">about making a copy of an object without reference in PHP</a> but that soluction is not working.</p>

<p>I want to take all elements of a collection, change one property (add months) and push that new element into a new collection.</p>

<p>So I've used <code>clone</code> in order to get a totally new instance of an <code>Invoice</code> wich should be affected just that instance, but is not.</p>

<p>With this code:</p>

<pre><code>while($thisInvoiceYear &lt;= CURRENT_YEAR) {
    $clonedInvoice = clone($thisInvoice);
    $this-&gt;extendedInvoices-&gt;push($clonedInvoice);
    $toSaveInvoice = $this-&gt;extendedInvoices-&gt;last();
    $thisInvoiceYear = $this-&gt;getCurrentYear($toSaveInvoice);
    $toSaveInvoice-&gt;Schedule-&gt;StartDate = Carbon::parse($toSaveInvoice-&gt;Schedule-&gt;StartDate)-&gt;addMonths($period);
    unset($clonedInvoice);
}
</code></pre>

<p>Where <code>$thisInvoice</code> is an object of the Invoice collection, <code>$this-&gt;extendedInvoices</code> is a method of the class Invoice.</p>

<p>The problem is that every change done in <code>$toSaveInvoice</code> also affects to the original collection element. After 3 iterations, date is the same for all elements of extendedInvoices and should not be:</p>

<p><a href="https://i.stack.imgur.com/R6TTh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R6TTh.png" alt="enter image description here"></a></p>

<p>I am using Laravel 5.2, but I think there aren't helpers to clone an element of a collection.</p>

<p>Do you know why <code>clone()</code> is not working? Thanks a lot for your help.</p>