Prototype of php design pattern

Source: Internet
Author: User
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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.