Try to execute following code: why the container object has been duplicated while the contained one has been referenced? How can I avoid this (doing a full copy of the original?)
class CObject
{
var $subobj;
var $value;
function report() { return $this->value; }
}
$first = new CObject;
$first->value = "First object";
echo '$first->value = "'. $first->report(). '"<br>';
$first->content = new CObject;
$first->content->value = "Contained in the first object";
echo '$first->content->value = "'. $first->content->report(). '"<br>';
/////////////////
$second = $first;
/////////////////
echo '<br>After assignment:<br><br>';
$second->value = "Second object";
echo '$second->value = "'. $second->report(). '"<br>';
$second->content->value = "Contained in the second object";
echo '$second->content->value = "'. $second->content->report(). '"<br>';
echo '<br>while<br><br> $first->value = "'. $first->report(). '"<br>';
$first->report();
echo '<br><b>BUT!</b>: $first->content->value = "'. $first->content->report(). '"...WHY???<br>';
class CObject
{
var $subobj;
var $value;
function report() { return $this->value; }
}
$first = new CObject;
$first->value = "First object";
echo '$first->value = "'. $first->report(). '"<br>';
$first->content = new CObject;
$first->content->value = "Contained in the first object";
echo '$first->content->value = "'. $first->content->report(). '"<br>';
/////////////////
$second = $first;
/////////////////
echo '<br>After assignment:<br><br>';
$second->value = "Second object";
echo '$second->value = "'. $second->report(). '"<br>';
$second->content->value = "Contained in the second object";
echo '$second->content->value = "'. $second->content->report(). '"<br>';
echo '<br>while<br><br> $first->value = "'. $first->report(). '"<br>';
$first->report();
echo '<br><b>BUT!</b>: $first->content->value = "'. $first->content->report(). '"...WHY???<br>';
Comment