Section 5 - Cloning the object model in PHP5 calls the object by reference, but sometimes you may want to build a copy of an object, and hope that the original object change does 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, the __clone method will create a object with the original object with the same properties and methods. If you want to change during cloning The default content, you have to write (attributes or methods) in __clone. Clone method can have no parameters, but it also contains this and That pointer (That pointing to the object). If you choose to clone yourself, you want Carefully copy any information you want your object, from That to this. If you use __clone to copy. PHP does not perform any hidden copy, the following shows an example of the changing number of objects:
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 $ this-> 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 "); $ OT2 = Clone $ OT; // Out: 1 ZEEV's Object Print ($ OT-> getId ()." "" $ Ot-> getname (). ""; // Output: 2 Clone of Zeev's Object Print ($ OT2-> GetId (). "" "$ Ot2-> getName ()." ");?>