单例模式创建 mysqli 数据库链接的单例对象

class Db
{
    private static $instance;
    public $handle;
    private function __construct($host, $username, $password, $dbname)
    {
        $this->handle = NULL;
        $this->getcon($host, $username, $password, $dbname);
    }
    public static function getBb()
    {
        self::$instance = new Db();
        return self::$instance;
    }
    private function getcon($host, $username, $password, $dbname)
    {
        if($this->handle!=NULL){
            return true;
        }
        $this->handle = mysqli_connect($host, $username, $password, $dbname);
    }
}
点击此处
隐藏目录