Section 5 - Cloning
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. For this purpose, PHP defines A special method called __clone. Like __construct and __destruct, there are two underscores in front.
By default, using the __clone method will create an object with the same properties and methods as the original object. If 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:
Green c hinaip ower.com7AT0x
class ObjectTracker //Object tracker
{
private static $nextSerial = 0;
private $id;
private $name;
function __construct($name) //Constructor function
$this->name = $name; >id = ++self::$nextSerial;
; ;
$this->id = ++self::$nextSerial; ;id);
}
http://www.bkjia.com/PHPjc/445143.html
www.bkjia.com
true