Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between deep copy and shallow copy in php

Shulou Source: shulou.com Published: 2022-06-01 18:39:03 09月28日 Update

This article introduces the knowledge about "what is the difference between deep copy and shallow copy in php". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

The difference between deep copy and shallow copy in PHP: 1. Deep copy assignment is a complete copy, while shallow copy is only a reference assignment, which is equivalent to taking an alias;2. If a deep copy changes one of them, it will not affect the other, while a shallow copy will affect the other.

Operating environment of this tutorial: Windows 10 system, PHP7.1 version, DELL G3 computer

What is the difference between deep copy and shallow copy in PHP?

Let's talk about deep copy and shallow copy popular understanding

A complete copy, in which a change to one does not affect the other.

Shallow copy: assignment, reference assignment, equivalent to taking an alias. Changing one affects the other.

In PHP, when = is assigned, ordinary objects are deep copies, but for objects, shallow copies. That is, 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 the parameter is preceded by an ampersand when the function is defined)

In php4, the = assignment of an object is to implement a copy, which has many problems. Without knowing it, we may copy many copies.

In php5,= assignments and passes of objects are references. To implement a copy, php provides an implementation of the clone function.

clone made a complete copy. But with clone, we might not want to copy everything from the source object, so we can do that with__clone.

In__clone (), we can do something. Note that these operations, namely the__clone function, are performed on the copied replica object.

Because of the assignment-time reference of objects, php provides clone functions to copy objects to achieve value copying.

However, clone function has such a problem. When cloning an object, the ordinary attribute energy value of the original object is copied, but the object attribute assignment of the source object is still a reference assignment, a shallow copy.

To achieve true deep copy of an object, there are two ways:

Write the clone function as follows:

It is not convenient to rewrite the__clone() function, and you have to clone the object properties of each class in__clone()

The second method, using serialization and deserialization, is simple to implement deep copies of objects without modifying classes.

There is also a third method, which is actually similar to the second method. After json_encode, json_decode, implement assignment.

"php deep copy and shallow copy what is the difference" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: Copy object property normal function influence output sequence method copy or content that is aliases parameters more knowledge problems practical popular Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Xiaomi Microsoft Docker