PHP object replication, is a copy of the object's reference address, so use $objA = $objB this style, $objA and $OBJB will point to the same memory address. $objB can also be affected when the $obja is changed.
If we want to replicate $obja objects as $OBJB objects, what changes $objA after replication will not affect $OBJB. That is, $obja and $OBJB are two separate objects, but $OBJB's initial values are created by $obja, and the more efficient approach is to use the Clone () method.
$objB = Clone $objA;
The $objB value is an instance of the Clone () method changed in the $obja base plus the $obja case object.
When an object is copied, the references in all properties remain unchanged, point to the original variable, and if the __clone () method is defined, the __clone () method in the newly created object is invoked and can be used to modify the value of the property.
Example 1:clone Object
<?php
class subclass{
private $name;
Private $age;
Public Function __construct () {
$this->name = ' fdipzone ';
$this->age = ' a ';
}
Public Function __clone () {
$this->name = ' xfdipzone ';
$this->age = ' i ';
}
}
Class myclass{public
$p 1;
Public $p 2;
Public Function __construct () {
} public
function __clone () {
$this->p1 = Clone $this->p1;
}
}
$obj = new MyClass ();
$obj->P1 = new Subclass ();
$obj->p2 = new Subclass ();
$obj 2 = clone $obj;
Echo ' <pre> ';
Echo ' obj<br> ';
Var_dump ($obj);
Echo ' <br>obj2<br> ';
Var_dump ($obj 2);
echo ' OK ';
Echo ' </pre> ';
? >
The example above will output:
Obj
object (MyClass) #1 (2) {
[P1]]=>
object (Subclass) #2 (2) {
[' name ': ' Subclass ':p rivate]=>
string (8) "Fdipzone"
[' Age ': ' Subclass ':p rivate]=>
string (2) '
}
[' P2 ']=>
object (Subclass) #3 (2) {
[] Name ': ' Subclass ':p rivate]=>
string (8) ' Fdipzone '
[' age ': ' Subclass ':p rivate]=>
string (2) ' 30 '
}
}
obj2
object (MyClass) #4 (2) {
[' P1 ']=>
object (Subclass) #5 (2) {
[' name ' : "Subclass":p rivate]=>
string (9) "Xfdipzone"
["Age": "Subclass":p rivate]=>
string (2) "29"
}
[' P2 ']=>
object (Subclass) #3 (2) {
[' name ': ' Subclass ':p rivate]=>
string (8) ' Fdipzone '
[] Age ': ' Subclass ':p rivate]=>
string (2) '}
}