PHP single-instance mode implementation

Source: Internet
Author: User
  1. /**
  2. *
  3. * User:jifei
  4. * DATE:2013-07-31
  5. * time:23:19
  6. */
  7. /**
  8. * Class Singleton
  9. * Single-case mode, also known as the list mode, is a common software design mode. When applying this pattern, the class of the Singleton object must ensure that only one instance exists,
  10. * Fully embodies the thought of DRY (Don ' t Repeat yourself).
  11. *
  12. * The idea of implementing a singleton pattern is that a class can return an object with a reference (always the same) and a method that obtains the instance (must be a static method, usually using the name getinstance);
  13. * When we call this method, return this reference if the class holds a reference that is not empty, create an instance of the class if the class keeps a reference empty, and give the instance a reference to the class;
  14. * At the same time we define the constructor of the class as a private method, so that other code cannot instantiate the class's object by invoking its constructor, only the static method provided by the class to get the unique instance of the class.
  15. *
  16. * Application Scenario: A scenario that applies to only one instance of a class. Database connection, logging, shopping cart
  17. * Cons: PHP runs at the page level and cannot directly implement cross-page memory data sharing.
  18. */
  19. Class Singleton
  20. {
  21. To save a private static member variable for a class instance
  22. private static $_instance;
  23. Private method of construction
  24. Private Function __construct ()
  25. {
  26. Echo ' This is a constructed method;
  27. }
  28. Create an empty private __clone method to prevent the object from being cloned
  29. Private Function __clone ()
  30. {
  31. }
  32. A singleton method for obtaining a unique instance object
  33. public static function getinstance ()
  34. {
  35. if (! ( Self::$_instance instanceof Self)) {
  36. Instanceof is used to detect the dependencies of an object to a class, is_subclass_of whether the class belongs to a subclass of the class
  37. Self::$_instance = new self ();
  38. }
  39. return self::$_instance;
  40. }
  41. Test
  42. Public Function test ()
  43. {
  44. Echo 123;
  45. }
  46. }
  47. $a = singleton::getinstance ();
  48. $a->test ();
  49. Echo Php_eol;
  50. $b = Singleton::getinstance (); Construction method is not executed on second call
  51. $b->test ();
  52. Echo Php_eol;
  53. $c =new Singleton (); Because the construction method is private, this will be an error.
  54. $d =clone $a; Clone object error
Copy Code
Php
  • 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.