Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @copyright Copyright (C) 2006-2016 Xue Can <xuecan@gmail.com> and contributors.
- */
- namespace Singleton;
- /**
- * Singleton Design Pattern
- *
- * THIS IS CONSIDERED TO BE AN ANTI-PATTERN!
- * FOR BETTER TESTABILITY AND MAINTAINABILITY USE DEPENDENCY INJECTION!
- *
- * @author Xue Can <xuecan@netpas.co>
- */
- class Singleton
- {
- /**
- * @var Singleton[] The reference to *Singleton* instances of any child class.
- */
- private static $instances = [];
- /**
- * Returns the *Singleton* instance of this class.
- *
- * @return static The *Singleton* instance.
- */
- public static function getInstance()
- {
- if (!isset(self::$instances[static::class])) {
- self::$instances[static::class] = new static();
- }
- return self::$instances[static::class];
- }
- /**
- * Protected constructor to prevent creating a new instance of the
- * *Singleton* via the `new` operator from outside of this class.
- */
- protected function __construct()
- {
- }
- /**
- * Private clone method to prevent cloning of the instance of the
- * *Singleton* instance.
- *
- * @return void
- */
- final private function __clone()
- {
- }
- /**
- * Private unserialize method to prevent unserializing of the *Singleton*
- * instance.
- *
- * @return void
- */
- final private function __wakeup()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement