/* + Tips + | this document is Haohappy-read | notes in the ClassesandObjects chapter | translation-oriented + personal experiences | do not repost the notes to avoid unnecessary troubles. thank you |
/* + --------------------------------------------------------------------------------- + | = This article is Haohappy read < > | = Notes in the middle Classes and Objects chapter | = Translation + personal experience | = do not repost to avoid unnecessary troubles, thank you | = Welcome to criticize and correct, hope to make progress together with all PHP enthusiasts! + Example + */section 5-clone the object model in PHP5 to call the object through reference, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone. like _ construct and _ destruct, the front has two underscores. by default, the _ clone method is used to create an object with the same attributes and methods as the original object. if you want to change the default content during cloning, overwrite (attribute or method) in _ clone ). the clone method can have no parameters, but it also contains the this and that pointers (that points to the copied object ). if you choose to clone yourself, you must be careful to copy any information contained in your object, from that to this. if you use _ clone to copy. PHP does not execute any implicit replication. the following shows an example of how to automate objects using sequence numbers: Name = $ name; $ this-> id = + self: $ nextSerial;} function _ clone () // Clone {$ this-> name = "Clone of $ that-> name"; $ this-> id = + self: $ nextSerial;} function getId () // obtain the value of the id attribute {return ($ this-> id);} function getName () // obtain the value of the name attribute {return ($ this-> name) ;}}$ ot = new ObjectTracker ("Zeevs Object "); $ ot2 = $ ot->__ clone (); // output: 1 Zeevs Object print ($ ot-> getId (). "". $ ot-> getName ()."
"); // Output: 2 Clone of Zeevs Object print ($ ot2-> getId ()." ". $ ot2-> getName ()."
");?>