class Singleton { /** * @var array */ private static $instances = []; /** * Prevent direct instantiation. */ private function __construct() { } /** * Get the instance of this class. *
* @return mixed */ final public static function getInstance() { $classname = get_called_class(); if (! isset(self::$instances[$classname])) { self::$instances[$classname] = new static(); } return self::$instances[$classname]; } /** * Prevent cloning. * * @throws \RuntimeException */ final public function __clone() { throw new \RuntimeException('Clone is not allowed for ' . get_class($this)); } /** * Prevent unserialization. * * @throws \RuntimeException */ final public function __wakeup() { throw new \RuntimeException('Unserialization is not allowed for ' . get_class($this)); }}//self 当前函数所在类 static 当前调用的类名
php单例模式
最新推荐文章于 2023-07-30 10:37:22 发布