Home > Backend Development > PHP Tutorial > PHP singleton pattern implementation

PHP singleton pattern implementation

WBOY
Release: 2016-07-25 08:42:16
Original
918 people have browsed it
  1. /**
  2. *
  3. * User: jifei
  4. * Date: 2013-07-31
  5. * Time: 23:19
  6. */
  7. /**
  8. * Class Singleton
  9. * Singleton pattern, also called monad pattern, is a commonly used software design pattern. When applying this pattern, the class of the singleton object must ensure that only one instance exists,
  10. * fully embodies the idea of ​​DRY (Don't Repeat Yourself).
  11. *
  12. * The idea of ​​implementing the singleton pattern is: a class can return a reference to the object (always the same one) and a method to obtain the instance (must be a static method, usually using the name getInstance);
  13. * When we call In this method, if the reference held by the class is not empty, the reference will be returned. If the reference held by the class is empty, an instance of the class will be created and the reference of the instance will be assigned to the reference held by the class;
  14. * At the same time, we will also The constructor of a class is defined as a private method, so that code elsewhere cannot instantiate objects of the class by calling the constructor of the class. The only instance of the class can only be obtained through the static methods provided by the class.
  15. *
  16. * Application scenario: Suitable for scenarios where a class has only one instance. Database connection, logging, shopping cart
  17. * Disadvantages: PHP runs at the page level and cannot directly share memory data across pages.
  18. */
  19. class Singleton
  20. {
  21. //Save the private static member variable of the class instance
  22. private static $_instance;
  23. //Private constructor method
  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. //Single case method, used to get the only instance object
  33. public static function getInstance()
  34. {
  35. if (!(self::$_instance instanceof self)) {
  36. //instanceof is used to detect objects and classes affiliation, is_subclass_of whether the class to which the object belongs is 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(); //On the second call The constructor is not executed
  51. $b->test();
  52. echo PHP_EOL;
  53. //$c=new Singleton(); Since the constructor is private, this will report an error
  54. //$d=clone $a; Clone object Report an error
Copy code

PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template