Prototype of php design pattern
Name = $ name;} public function setName ($ name) {$ this-> name = $ name;} public function getName () {return $ this-> name ;} /* shallow copy: all variables of the copied object contain the same value as the original object, and references to other objects still point to the original object. That is, the shortest copy is only responsible for the current object instance and does not copy the referenced object. Deep copy: all the variables of the copied object contain the same value as the original object, except those variables that reference other objects. Variables that reference other objects will point to a new object to be copied, instead of the original referenced objects. That is, the deep copy operation copies all the objects referenced by the objects to be copied. this kind of copy operation is called indirect copy. How many layers do deep copy have to go deep. When you decide to copy an object in the deep copy mode, you must decide whether to use the shortest copy or deep copy for the indirect copy object or continue to use the deep copy object. Therefore, when deep copy is adopted, the depth must be determined to be deep. In addition, circular references may occur during deep copy. Deep copy by serialization: deep copy by serialization. the process of writing an object to a stream is a serialization process, however, in the industry, the serialization process is called "Frozen" or "pickles"; */public function copy () {// potential copy // return clone $ this; // Deep copy $ serialize_obj = serialize ($ this); $ clone_obj = unserialize ($ serialize_obj); return $ clone_obj ;}} $ pro = new \ Tools \ sonPrototype ("prototype1
"); Echo $ pro-> getName (); $ pro2 = clone $ pro; $ pro2-> setName (" prototype2
"); Echo $ pro2-> getName (); $ pro3 = $ pro-> copy (); $ pro3-> setName (" prototype3
"); Echo $ pro3-> getName ();