Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Singleton.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik;
  10. /**
  11. * The singleton base class restricts the instantiation of derived classes to one object only.
  12. *
  13. * All plugin APIs are singletons and thus extend this class.
  14. *
  15. * @api
  16. */
  17. class Singleton
  18. {
  19. protected static $instances;
  20. protected function __construct() { }
  21. final private function __clone() { }
  22. /**
  23. * Returns the singleton instance for the derived class. If the singleton instance
  24. * has not been created, this method will create it.
  25. *
  26. * @return Singleton
  27. */
  28. public static function getInstance() {
  29. $class = get_called_class();
  30. if (!isset(self::$instances[$class])) {
  31. self::$instances[$class] = new $class;
  32. }
  33. return self::$instances[$class];
  34. }
  35. /**
  36. * Used in tests only
  37. * @ignore
  38. */
  39. public static function unsetInstance()
  40. {
  41. $class = get_called_class();
  42. unset(self::$instances[$class]);
  43. }
  44. /**
  45. * Sets the singleton instance. For testing purposes.
  46. * @ignore
  47. */
  48. public static function setSingletonInstance($instance)
  49. {
  50. $class = get_called_class();
  51. self::$instances[$class] = $instance;
  52. }
  53. }