首页 > 技术文章 > php的单例模式

phpzhou 2016-11-03 14:30 原文

class DataBase
{
    private static $_instance;

    /**
     * 私有化构造方法,不能直接new对象
     * DataBase constructor.
     */
    private function __construct()
    {
    }

    /**
     * 私有化克隆方法,不能复制对象
     */
    private function __clone()
    {
    }

    /**
     * 此方法为唯一调用入口
     * @return DataBase
     */
    public static function getInstance()
    {
        if(!self::$_instance instanceof self)
        {
            self::$_instance = new self;
        }
        return self::$_instance;
    }
}

 

推荐阅读