Home > Article > Backend Development > What is the difference between deep copy and shallow copy in php
The difference between deep copy and shallow copy in PHP: 1. Deep copy is a complete copy when assigning, while shallow copy is just a reference assignment, which is equivalent to taking an alias; 2. If deep copy makes a Changes will not affect the other, while shallow copy modifications to one will affect the other.
The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer
Let’s first talk about the popular understanding of deep copy and shallow copy
Deep copy: the value during assignment is completely copied, complete copy, making changes to one of them will not affect the other
Shallow copy: When assigning, reference assignment is equivalent to taking an alias. Modifications to one of them will affect the other
In PHP, when = is assigned, ordinary objects are deep copies, but for objects, they are shallow copies. In other words, the assignment of an object is a reference assignment. (When an object is passed as a parameter, it is also passed by reference, regardless of whether there is an ampersand in front of the parameter when the function is defined)
In php4, the = assignment of the object is to implement a copy, which has many problems. We may make many copies.
In PHP5, the = assignment and transfer of objects are both references. To implement a copy, PHP provides the clone function implementation.
clone makes a complete copy. But when cloning, we may not want to copy all the contents of the source object, then we can use __clone to operate.
In __clone(), we can perform some operations. Note that these operations, that is, the __clone function operates on the copied copy object
<?php //普通对象赋值,深拷贝,完全值复制 $m = 1; $n = $m; $n = 2; echo $m;//值复制,对新对象的改变不会对m作出改变,输出 1.深拷贝 echo PHP_EOL; /*==================*/ //对象赋值,浅拷贝,引用赋值 class Test{ public $a=1; } $m = new Test(); $n = $m;//引用赋值 $m->a = 2;//修改m,n也随之改变 echo $n->a;//输出2,浅拷贝 echo PHP_EOL; ?>
Since the object is referenced during assignment, in order to realize value copying, PHP provides the clone function to copy the object.
But there is a problem with the clone function. When an object is cloned, the ordinary attributes of the original object can be copied by value, but when the object attributes of the source object are assigned, they are still reference assignments and shallow copies.
<?php class Test{ public $a=1; } class TestOne{ public $b=1; public $obj; //包含了一个对象属性,clone时,它会是浅拷贝 public function __construct(){ $this->obj = new Test(); } } $m = new TestOne(); $n = $m;//这是完全的浅拷贝,无论普通属性还是对象属性 $p = clone $m; //普通属性实现了深拷贝,改变普通属性b,不会对源对象有影响 $p->b = 2; echo $m->b;//输出原来的1 echo PHP_EOL; //对象属性是浅拷贝,改变对象属性中的a,源对象m中的对象属性中a也改变 $p->obj->a = 3; echo $m->obj->a;//输出3,随新对象改变 ?>
To achieve a true deep copy of an object, there are two methods:
Write the clone function: as follows
<?php class Test{ public $a=1; } class TestOne{ public $b=1; public $obj; //包含了一个对象属性,clone时,它会是浅拷贝 public function __construct(){ $this->obj = new Test(); } //方法一:重写clone函数 public function __clone(){ $this->obj = clone $this->obj; } } $m = new TestOne(); $n = clone $m; $n->b = 2; echo $m->b;//输出原来的1 echo PHP_EOL; //可以看到,普通属性实现了深拷贝,改变普通属性b,不会对源对象有影响 //由于改写了clone函数,现在对象属性也实现了真正的深拷贝,对新对象的改变,不会影响源对象 $n->obj->a = 3; echo $m->obj->a;//输出1,不随新对象改变,还是保持了原来的属性 ?>
It is not convenient to rewrite the __clone() function. And you have to put all the object attributes in this class in __clone() in each class - clone
The second method is to use serialization and deserialization. This method realizes the object Deep copy is simple and does not need to modify the class
<?php class Test{ public $a=1; } class TestOne{ public $b=1; public $obj; //包含了一个对象属性,clone时,它会是浅拷贝 public function __construct(){ $this->obj = new Test(); } } $m = new TestOne(); //方法二,序列化反序列化实现对象深拷贝 $n = serialize($m); $n = unserialize($n); $n->b = 2; echo $m->b;//输出原来的1 echo PHP_EOL; //可以看到,普通属性实现了深拷贝,改变普通属性b,不会对源对象有影响 $n->obj->a = 3; echo $m->obj->a;//输出1,不随新对象改变,还是保持了原来的属性,可以看到,序列化和反序列化可以实现对象的深拷贝 ?>
There is also a third method, which is actually similar to the second method. json_encode and then json_decode to achieve assignment
Recommended learning: "PHP Video tutorial》
The above is the detailed content of What is the difference between deep copy and shallow copy in php. For more information, please follow other related articles on the PHP Chinese website!