Welcome to the Linux community forum and interact with 2 million technicians. In PHP5, the assignment of objects is considered as address reference by default. For example :? Php $ objB $ objA ;? For the above Code, let's take a look at the memory allocation structure: From the above we can see that when the $ objA object is defined, the system will allocate a memory for it (①
Welcome to the Linux community forum to interact with 2 million technical staff> In PHP5, the assignment of objects is considered as address reference by default, for example :? Php $ objB = $ objA ;? For the above Code, let's take a look at the memory allocation structure: From the above we can see that when the $ objA object is defined, the system will allocate a memory for it (①
Welcome to the Linux community forum and interact with 2 million technicians>
In PHP5, the assignment between objects is considered as address reference by default. For example:
$ ObjB = $ objA;
?>
For the above Code, let's take a look at the memory allocation structure:
We can see from the above that when defining $ objA objects, the system will allocate a piece of memory for them (① arrow). When $ objB is assigned $ objA, the system will not reallocate the memory, instead, we direct the reference of $ objB to $ objA memory. In fact, these two objects point to the same memory. When we modify the value of $ objB, the value of $ objA also changes, so that if we need two identical and relatively independent objects, we need to define them separately, which is difficult to operate, and the readable line of the program is not good. To solve this problem, PHP provides us with a method to clone objects. The method described in the PHP manual is relatively simple, the following example shows how to use the clone method.
Example 1:
Class subclass {
Private $ name;
Private $ age;
Public function _ construct (){
$ This-> name = 'zhang san ';
$ This-> age = '30 ';
}
Public function _ clone (){
$ This-> name = 'Li si ';
$ This-> age = '29 ';
}
}
Class myclass {
Public $ p1;
Public $ p2;
Public function _ construct (){
}
Public function _ clone (){
$ This-> p1 = clone $ this-> p1;
}
}
$ Obj = new myclass ();
$ Obj-> p1 = new subclass ();
$ Obj-> p2 = new subclass ();
$ Obj2 = clone $ obj;
Echo'
';Echo 'This is obj
';
Var_dump ($ obj );
Echo '-----------------------';
Echo'
This is obj2
';
Var_dump ($ obj2 );
Echo'
';
?>
Output:
This is objobject (myclass) #1 (2 ){
["P1"] =>
Object (subclass) #2 (2 ){
["Name": "subclass": private] =>
String (4) "James"
["Age": "subclass": private] =>
String (2) "30"
}
["P2"] =>
Object (subclass) #3 (2 ){
["Name": "subclass": private] =>
String (4) "James"
["Age": "subclass": private] =>
String (2) "30"
}
}
----------------------- This is obj2object (myclass) #4 (2 ){
["P1"] =>
Object (subclass) #5 (2 ){
["Name": "subclass": private] =>
String (4) "Li Si"
["Age": "subclass": private] =>
String (2) "29"
}
["P2"] =>
Object (subclass) #3 (2 ){
["Name": "subclass": private] =>
String (4) "James"
["Age": "subclass": private] =>
String (2) "30"
}
}
You can see that $ obj2 clone $ obj, $ obj-> p1 clone $ obj-> p1, the _ clone () method is executed. In the _ clone method, the name and age attributes of p1 are modified, so the name and age of p1 change. P2 does not execute the clone () method, so the newly copied $ obj2-> p2 has the same attributes as $ obj-> p2.
[1] [2]