Let's talk about deep-copy and shallow-copy popular understanding
Deep copy: Assignment value is fully copied, full copy, change to one of them, does not affect another
Shallow copy: When assigned, the reference is assigned, which is equivalent to taking an alias. One of the modifications will affect the other
in PHP, = when assigned, the normal object is a deep copy, but to the object it is a shallow copy. In other words, the assignment of an object is a reference assignment . (When an object is passed as a parameter, it is also a reference pass, regardless of whether the parameter is preceded by A & symbol in the function definition)
<?php//Normal object assignment, deep copy, full value copy $m = 1; $n = $m; $n = 2;echo $m;//value copy, change to new object does not change to M, Output 1. Deep copy echo php_eol;/*================== *///object assignment, shallow copy, reference assignment class test{public $a = 1;} $m = new Test (); $n = $m;//reference Assignment $m->a = 2;//Modify M,n also change echo $n->a;//output 2, shallow copy echo php_eol; >
PHP provides the clone function to implement a copy of the object, because it is referenced when the object is assigned, and if you want to replicate the value.
However, there is a problem with the clone function, when cloning an object, the normal property of the original object can be copied, but the object property of the source object is assigned value or reference assignment, shallow copy.
<?phpclass test{public $a = 1;} Class Testone{public $b =1;public $obj;//contains an object property, clone, which is a shallow copy of public function __construct () {$this->obj = new Test ( );}} $m = new Testone (); $n = $m;//This is a completely shallow copy, whether the normal property or object property $p = Clone $m;//The normal attribute implements a deep copy, changes the normal attribute B, does not affect the source object $p->b = 2;echo $m->b; The output of the original 1echo php_eol;//object property is a shallow copy, changing the object properties of a, the object property in the source object m in the properties of a also changed $p->obj->a = 3;echo $m->obj->a;//output 3, with the new object changed? >
There are two ways to achieve a true deep copy of an object:
Write the Clone function: the following
<?phpclass test{public $a = 1;} Class Testone{public $b =1;public $obj;//contains an object property, clone, which is a shallow copy of public function __construct () {$this->obj = new Test ( );} Method One: Override the Clone function Public function __clone () {$this->obj = clone $this->obj;}} $m = new Testone (), $n = Clone $m, $n->b = 2;echo $m->b;//Output The original 1echo php_eol;//can be seen, the normal properties of the deep copy, change the normal property B, will not affect the source object// Since the clone function has been rewritten, now the object properties also implement a true deep copy, the changes to the new object will not affect the source object $n->obj->a = 3;echo $m->obj->a;//Output 1, not with the new object changes, or to maintain the original properties? >
It is inconvenient to overwrite the __clone () function, and you have to have the object properties in this class in each class in __clone () one by one clone
The second method, which is implemented by serialization deserialization, enables the deep copy of the object to be simple, without modifying the class
<?phpclass test{public $a = 1;} Class Testone{public $b =1;public $obj;//contains an object property, clone, which is a shallow copy of public function __construct () {$this->obj = new Test ( );}} $m = new Testone ();//Method Two, serialization deserialization implements deep copy of Object $n = Serialize ($m); $n = Unserialize ($n); $n->b = 2;echo $m->b;//output original 1echo PH p_eol;//can see that the normal properties of the deep copy, change the normal property B, will not affect the source object $n->obj->a = 3;echo $m->obj->a;//Output 1, do not change with the new object, or maintain the original properties, you can see , serialization and deserialization enable deep copy of Objects?>
Deep copy and shallow copy of objects in PHP