/*
+------------------------------------------------ ----------------------------------+
| = This article is read by Haohappy<
| = Notes from the Chapter Classes and Objects
| = Translation + personal experience
| = To avoid unnecessary trouble that may occur, please do not reprint, thank you
| = Criticisms and corrections are welcome, and I hope that all PHP enthusiasts Make progress together!
| = PHP5 Research Center: http://blog.csdn.net/haohappy2004
+-------------------------- -------------------------------------------------- ---+
*/
Section 5--Clone
The object model in PHP5 calls objects by reference, but sometimes you may want to create a copy of the object, and hope that changes to the original object will not affect the copy. In order For this purpose, PHP defines a special method called __clone. Like __construct and __destruct, there are two underscores in front of it.
By default, using the __clone method will create an object with the same properties and methods as the original object Object. If you want to change the default content when cloning, you have to override (properties or methods) in __clone.
The clone method can have no parameters, but it contains both this and that pointers (that points to the copied object). If you choose to clone yourself, you need to be careful to copy any information you want your object to contain, from that to this. If you use __clone to copy. PHP will not perform any implicit copying,
shown below An example of using serial numbers to automate objects:
Copy code The code is as follows:
class ObjectTracker //Object tracker
{
private static $nextSerial = 0;
. ::$nextSerial;
}
() //Clone
Function getId() / / Get the value of the id attribute Return($this->name);
}
}
$ot = new ObjectTracker("Zeev's Object");
$ot2 = $ot->__clone();
//Output: 1 Zeev's Object
print($ot->getId() . " " . $ ot->getName() . "
");
//Output: 2 Clone of Zeev's Object
print($ot2->getId() . " " . $ot2->getName() . "
");
?>
The above introduces the official website download of the fifth generation automatic recharge software. Section 5 - Cloning, includes the content of the official website download of the fifth generation automatic recharge software. I hope it will be helpful to friends who are interested in PHP tutorials.