Objective
This article mainly introduces PHP programming in the __clone () method using the detailed, __clone () method equivalent to a shallow copy, is the basic knowledge of PHP primer learning, the need for friends can refer to.
1 objects are reference data types, when you assign an object to another object by using =, the address of the object is assigned, two variables point to the same address, and the other one changes
__construct () Function: Creates a new SimpleXMLElement object. If successful, the function returns an object. If it fails, it returns false.
Original $zhangsan:
Class person{public $name; public $age; function __construct ($name, $age) { $this->name= $name; $this->age= $age; }} $zhangsan =new person ("Zhang San"), Var_dump ($zhangsan);
Set up a Lisi
$lisi = $zhangsan;
Set the age of Lisi to 28.
$lisi->age=28;
Printing Zhang San and John Doe at the same time will show:
2clone: If you want to completely clone an object to a non-interfering object, you need to use the Clone keyword;
Put John Doe clone Zhang San and set age to 28.
$lisi =clone $zhangsan; $lisi->age=28;
Print Zhang San and John Doe at this time.
3__clone ();
① the Magic method is automatically invoked when cloning an object using the Clone keyword;
The ②__clone () function, which is equivalent to the constructor for cloning an object, is used to assign an initial value to a new cloned object;
③ in the Clone () function, the $this points to the object that is new to Cologne.
function __clone () { echo "execute clone command <br>"; $this->name= "John Doe"; }
@ Only Yun Hee
Getting Started with PHP, clone and __clone