Consider the following:
$object1 = new stdClass();
$object2 = $object1;
$object3 = clone $object1;
$object1->content = 'Ciao';
var_dump($object1);
// Outputs object(stdClass)#1 (1) { ["content"]=> string(4) "Ciao" }
var_dump($object2);
// Outputs object(stdClass)#1 (1) { ["content"]=> string(4) "Ciao" }
var_dump($object3);
// Outputs object(stdClass)#2 (0) { }
Is it a normal PHP behavior that $object2 has a content identical to $object1 ?
To me it sound like $object2 is a reference to $object1 instead of a copy.
Cloning the object before changing the content does act like a copy.
This behavior is different than what happens with variables and seems unintuitive to me.
Yes, that's normal. Objects are always "assigned" by reference in PHP5. To actually make a copy of an object, you need to clone it.
To be more correct though, let me quote the manual:
As of PHP5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
That's normal and I won't consider this unintuitive (for object instances):
$object1 = new stdClass();
Assigns a new object instance to $object1.
$object2 = $object1;
Assigns the object instance to $object2.
$object3 = clone $object1;
Assigns an new object instance cloned from an existing object instance to $object3.
If it would not be that way, each time you need to pass a concrete object instance, you would need to pass it by reference. That's burdensome at least but PHP did so in version 4 (compare zend.ze1_compatibility_mode core ). That was not useful.
object copy vs object clone
class test{
public $name;
public $addr;
}
// i create a object $ob
$ob=new test();
// object copy
$ob2=$ob;
// in object copy both object will represent same memory address
// example
$ob->name='pankaj raghuwanshi';
// i am printing second object
echo $ob2->name;
// output is : pankaj raghuwanshi
// another example
$ob2->name='raghuwanshi pankaj';
echo $ob->name;
// output is : raghuwanshi pankaj
// it means in copy of object original and copy object share same memory place
now clone of an object
$ob1=clone $ob;
echo $ob1->name; // output is : raghuwanshi pankaj
echo $ob->name; // output is : raghuwanshi pankaj
$ob1->name='PHP Clone';
$ob->name='PHP Obj';
echo $ob1->name; // output is : PHP Clone
echo $ob->name; // output is : PHP Obj
// on the base of these output we can say both object have their own memory space
// both are independent
Objects in php5 are essentially pointers, that is, an object variable contains only an address of the object data located somewhere else. An assignment $obj1 = $obj2 only copies this address and doesn't touch the data itself. This may indeed appear counterintuitive, but in fact it's quite practical, because you only rarely need to have two copies of the object. I wish php arrays used the same semantics.
来源:https://stackoverflow.com/questions/6972860/object-copy-versus-clone-in-php