Detailed Explanation of Shallow Replication and Deep Replication in PHP

  • 2021-08-10 07:10:47
  • OfStack

Detailed Explanation of Shallow Replication and Deep Replication in PHP

Foreword:

Recently, I reviewed the knowledge of Design Pattern. When I saw Prototype Pattern, I noticed that there was a problem of shallow replication and deep replication. Here to sum up 1, remind yourself to pay more attention in the future 1.

Since PHP5, the new operator automatically returns a reference, and an object variable no longer holds the value of the entire object, but only an identifier to access the true object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, the other variable is not a reference to the original one, but they all hold copies of the same identifier that points to the true content of the same object.

Here's a chestnut:


class Example1
{
  public $name;

  public function __construct($name)
  {
    $this->name = $name;
  }
}

$ex1 = new Example('test1');// $ex1->name Now it is: test1
$ex2 = $ex1;// $ex2->name Now it is: test1

$ex2->name = 'test2';//  Modify like this 1 After going down, $ex1->name And $ex2->name Has become: test2

From the above example, we should be able to understand the concept of reference between objects, so let's move on and provide the keyword clone in php for object replication, or use the above class to demonstrate 1:


$ex1 = new Example('test1');// $ex1->name Now it is: test1
$ex2 = clone $ex1;//$ex2->name Now it is: test1

$ex2->name = 'test2';// Now $ex1->name Or test1, And $ex2->name Yes test2

You can see here that after clone, $ex1 and $ex2 are two different objects that have their own variable environments. However, it should be noted here that inside these two objects, you have data of value type. If you have reference type inside, the reference in the new object obtained through clone still points to the original reference. Here, the concepts of shallow replication and deep replication are extended:

Shallow replication: Use clone to replicate objects. This replication is called "shallow replication". All variables of the assigned object still have the same values as the original object, while all references to other objects still point to the original object.

Deep copy: All variables of the copied object have the same values as the original object, except those that refer to other objects.

The default use of clone is to make a shallow copy, so how can I make a deep copy?

Mode 1: Use the __clone method


public function __clone()
{
  $this->obj = new Obj();
}

This is very straightforward, but there is a very troublesome way to operate, that is, when the class contains multiple references, you need to reset them one by one in the __clone method. But also to deal with a number of circular reference problems. Is very complicated.

Mode 2: Use serialization (refrigeration and thawing)


$tmp = serialize($ex1);
$ex2 = unserialize($tmp);

The resulting $ex2 is a completely new object, a process also known as the "refrigeration" and "thawing" processes in java.

Serialization is a recursive process. We don't care how many objects and how many layers of objects are referenced inside the object. We can copy them completely. Way 2 is really yellow and violent, but I like it very much.

If you have any questions, please leave a message or go to this site community to exchange and discuss, thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: