The example in this article describes the cloning clone usage of PHP objects. Share to everyone for your reference, specific as follows:
Shallow clones: Is simply a clone of a non-object, non-resource data, in which the property in the object is stored as an object type, the cloning is incomplete
<?php
class b{public
$val = ten;
}
Class a{public
$val =;
public $b;
Public Function __construct () {
$this->b = new B ();
}
}
$obj _a = new A ();
$obj _b = Clone $obj _a;
$obj _a->val =;
$obj _a->b->val =;
Var_dump ($obj _a);
Echo ' <br> ';
Var_dump ($obj _b);
The results of the operation are as follows:
Object (A) [1] public '
val ' => int public
' B ' =>
object (b) [2] public
' val ' => int
Object (A) [3] public '
val ' => int to
' B ' =>
object (b) [2] public
' Val ' => int 40
Deep Cloning: all attribute data for an object is copied thoroughly, and the Magic method is used to __clone () and to implement deep cloning inside
<?php
class b{public
$val = ten;
}
Class a{public
$val =;
public $b;
Public Function __construct () {
$this->b = new B ();
}
Public Function __clone () {
$this->b = Clone $this->b;
}
}
$obj _a = new A ();
$obj _b = Clone $obj _a;
$obj _a->val =;
$obj _a->b->val =;
Var_dump ($obj _a);
Echo ' <br> ';
Var_dump ($obj _b);
The results of the operation are as follows:
Object (A) [1] public '
val ' => int public
' B ' =>
object (b) [2] public
' val ' => int
Object (A) [3] public '
val ' => int to
' B ' =>
object (b) [4] public
' val ' => int 10
More about PHP Interested readers can view the site topics: "PHP object-oriented Program Design Primer", "PHP basic Grammar Introductory Course", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Array" operation Skills Encyclopedia, " Summary of PHP string usage, Introduction to PHP+MYSQL database operations, and a summary of PHP common database operations Tips
I hope this article will help you with the PHP program design.