Php design pattern learning series (7)-prototype object

Source: Internet
Author: User
: This article mainly introduces the php design pattern learning series (7)-prototype objects. if you are interested in PHP tutorials, refer to it. Disclaimer: Cheng Jie, author of this series of blog references "big talk Design Mode.

Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object. The Prototype mode allows an object to create another custom object. you do not need to know how to create any details. by passing a Prototype object to the object to be created, the objects to be created are created by requesting the prototype objects to copy themselves. It mainly faces the following problems: the creation of "some objects with complex structures"; these objects are often subject to drastic changes due to changes in requirements, however, they have stable and consistent interfaces.


In php, the class has implemented the prototype mode. php has a magic method _ clone () to clone an object like this.

Take a look at the UML class diagram:

Role Analysis:

1. abstract prototype, providing a clone interface

2. the specific prototype implements the clone interface.

Specific code:

[Php] view plain copy print?

  1. /** Abstract prototype
  2. * Class Prototype
  3. */
  4. Abstract class Prototype
  5. {
  6. Abstract function cloned ();
  7. }
  8. /** Specific prototype
  9. * Class Plane
  10. */
  11. Class Plane extends Prototype
  12. {
  13. Public $ color;
  14. Function Fly ()
  15. {
  16. Echo "flying and flying!
    ";
  17. }
  18. Function cloned ()
  19. {
  20. Return clone $ this;
  21. }
  22. }


Client Test code:

[Php] view plain copy print?

  1. Header ("Content-Type: text/html; charset = utf-8 ");
  2. // ------------------------ Prototype mode test code ------------------
  3. Require_once "./Prototype. php ";
  4. $ Plane1 = new Plane ();
  5. $ Plane1-> color = "Blue ";
  6. $ Plane2 = $ plane1-> cloned ();
  7. $ Plane1-> Fly ();
  8. $ Plane2-> Fly ();
  9. Echo "the color of plane1 is: {$ plane1-> color}
    ";
  10. Echo "the color of plane2 is: {$ plane2-> color}
    ";


Here is just a brief introduction to the core idea of the prototype mode. In fact, you can simply clone it in actual development.

$ Plane2 = clone $ plane1;

$ Plane2-> Fly ();

$ Plane2-> color;

The above introduces the php design pattern learning series (7)-prototype objects, including some content, hope to be helpful to friends who are interested in PHP tutorials.

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.