Learning notes for PHP Singleton mode

Source: Internet
Author: User
To achieve this purpose, you must ensure that only one instance exists in the project and provide an access point for global access. This requires you to call a shared static method of this class to access this instance, that is, this class cannot be instantiated outside the class,

To achieve this purpose, you must ensure that only one instance exists in the project and provide an access point for global access. in this case, you need to call a shared static method of this class to access this instance. that is to say, this class cannot be instantiated outside the class and can only be accessed within the class, in addition, instances can only be called through the shared static method;

I recently read a book on php advanced programming design, pattern, framework, and testing. I am very touched and want to share with you:

During project development, we usually want some class instances to be enough at a time, such as shared configuration classes, template operation classes, and database connections. These classes are common in the development of the entire project, if the instance is used for multiple times, the resource will be occupied.

To achieve this purpose, you must ensure that only one instance exists in the project and provide an access point for global access. in this case, you need to call a shared static method of this class to access this instance. that is to say, this class cannot be instantiated outside the class and can only be accessed within the class, in addition, instances can only be called through the shared static method;

So how can we ensure that this class cannot be instantiated outside? This singleton class must have a constructor _ construct and be set to private (because _ construct already exists, the default constructor is not considered: directly use the class name method), so that it will not be directly instantiated outside! At the same time, you also need to declare a static method variable to save such an instance, a shared static method to access this instance (because it is shared, so declared as static, storage of the shared code area in the memory ). you also need to create an empty private _ clone () method to prevent being cloned.

A typical singleton class is as follows:

The instance code is as follows:

  1. Class Simple
  2. {
  3. /**
  4. * Singleton instance
  5. *
  6. * @ Static
  7. * @ Var object Simple
  8. */
  9. Public static $ _ instance;
  10.  
  11. // Other member variables
  12.  
  13. /**
  14. * Constructor
  15. *
  16. * @ Return void
  17. */
  18. Private function _ construct ()
  19. {
  20. // Write constructor
  21. }
  22.  
  23. /**
  24. * Singleton mode call method
  25. *
  26. * @ Static
  27. * @ Return object Template
  28. */
  29. Public static function getInstance ()
  30. {
  31. If (! Self: $ _ instance instanceof self)
  32. Self: $ _ instance = new self ();
  33. Return self: $ _ instance;
  34. }
  35.  
  36. /**
  37. * Declare an empty private clone method
  38. *
  39. */
  40. Private function _ clone ()
  41. {
  42. }
  43.  
  44. // Other methods
  45. }
  46.  
  47. // Call method
  48. $ Simple = Simple: getInstance ();
  49. ?>

The instanceof keyword is used above. it is a comparison operator and returns a boolean value to determine whether an object instance is of a specified type, whether it inherits from a class, or whether it has implemented a specific interface.

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.