/*
+-------------------------------------------------------------------------------+
| = This is haohappy read < >
| = Notes in Classes and objects Chapter
| = Translation-based + personal experience
| = Please do not reprint in order to avoid the unnecessary trouble that may occur.
| = Welcome criticism, hope and all the PHP enthusiasts to progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/
Section Fifth-cloning
The object model in PHP5 invokes the object by reference, but sometimes you might want to make a copy of an object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone. Like __construct and __destruct, there are two underscores ahead.
By default, the __clone method will establish an object that has the same properties and methods as the original object. If you want to change the default content when cloning, you need to overwrite (attribute or method) in __clone.
A cloned method can have no arguments, but it contains both this and that pointer (that is, pointing to the object being copied). If you choose to clone yourself, you should be careful to copy any information you want your object to contain, from that to this. If you use __clone to copy. PHP does not perform any implicit replication,
The following shows an example of automating an object with series ordinals:
Copy the Code code as follows:
Class Objecttracker//Object Tracker
{
private static $nextSerial = 0;
Private $id;
Private $name;
function __construct ($name)//constructor
{
$this->name = $name;
$this->id = ++self:: $nextSerial;
}
function __clone ()//Clone
{
$this->name = "Clone of $that->name";
$this->id = ++self:: $nextSerial;
}
function getId ()//Get the value of the id attribute
{
Return ($this->id);
}
function GetName ()//Get the value of the Name property
{
Return ($this->name);
}
}
$ot = new Objecttracker ("Zeev ' s Object");
$ot 2 = $ot->__clone ();
Output: 1 Zeev ' s Object
Print ($ot->getid (). " " . $ot->getname (). "
");
Output: 2 Clone of Zeev ' s Object
Print ($ot 2->getid (). " " . $ot 2->getname (). "
");
?>
The above introduces the fifth generation of automatic top-up software download section fifth-cloning, including the fifth generation of automatic recharge software website download content, I hope that the PHP tutorial interested friends helpful.