php单例模式

来源:互联网 发布:现实爆炸录像软件 编辑:程序博客网 时间:2024/05/19 06:51
class Singleton{    /**     * @var array     */    private static $instances = [];    /**     * Prevent direct instantiation.     */    private function __construct()    {    }    /**     * Get the instance of this class.     *
//self 当前函数所在类 static 当前调用的类名
* @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)); }}

原创粉丝点击