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
The difference between deep copy and shallow copy in php is What
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
