Advertisement
Guest User

Untitled

a guest
May 30th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. <?php
  2. /**
  3. * @copyright Copyright (C) 2006-2016 Xue Can <xuecan@gmail.com> and contributors.
  4. */
  5.  
  6. namespace Singleton;
  7.  
  8. /**
  9. * Singleton Design Pattern
  10. *
  11. * THIS IS CONSIDERED TO BE AN ANTI-PATTERN!
  12. * FOR BETTER TESTABILITY AND MAINTAINABILITY USE DEPENDENCY INJECTION!
  13. *
  14. * @author Xue Can <xuecan@netpas.co>
  15. */
  16. class Singleton
  17. {
  18. /**
  19. * @var Singleton[] The reference to *Singleton* instances of any child class.
  20. */
  21. private static $instances = [];
  22.  
  23. /**
  24. * Returns the *Singleton* instance of this class.
  25. *
  26. * @return static The *Singleton* instance.
  27. */
  28. public static function getInstance()
  29. {
  30. if (!isset(self::$instances[static::class])) {
  31. self::$instances[static::class] = new static();
  32. }
  33. return self::$instances[static::class];
  34. }
  35.  
  36. /**
  37. * Protected constructor to prevent creating a new instance of the
  38. * *Singleton* via the `new` operator from outside of this class.
  39. */
  40. protected function __construct()
  41. {
  42. }
  43.  
  44. /**
  45. * Private clone method to prevent cloning of the instance of the
  46. * *Singleton* instance.
  47. *
  48. * @return void
  49. */
  50. final private function __clone()
  51. {
  52. }
  53.  
  54. /**
  55. * Private unserialize method to prevent unserializing of the *Singleton*
  56. * instance.
  57. *
  58. * @return void
  59. */
  60. final private function __wakeup()
  61. {
  62. }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement