Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report