NavigationUser login |
Simple PHP Copy vs Clone ExampleAssigning object instances results in assigning references:
<?php
class s {
public $state = 1;
}
$s = new s();
$t=$s;
$s->state=4;
echo "s state ".$s->state."\n";
echo "t state ".$t->state."\n";
s state 4 Using clone creates a shallow copy of the object. (Add a __clone() method to define your own deep copy. See http://php.net/manual/en/language.oop5.cloning.php)
<?php
class s {
public $state = 1;
}
$s = new s();
$t=clone $s;
$s->state=4;
echo "s state ".$s->state."\n";
echo "t state ".$t->state."\n";
s state 4 Note: Use debug_zval_dump() to see the reference count for a variable.
|
Recent blog posts
|