各位redis群的同学,帮忙查看指正一下代码,多谢了,希望看到各位的改进意见
<?php
class Singleton
{
// MYSQL数据库连接信息
const MYSQLHOSTNAME = "127.0.0.1";
const MYSQLCHARSET = "utf8";
private static $instances = array(); //伪类单例
private $db; //数据库连接句柄
/**
* Description:私有化构造函数,防止外界实例化对象
*/
private function __construct($dbname, $username, $password)
{
//连接数据库
$this->db = new mysqli(self::MYSQLHOSTNAME, $username, $password, $dbname);
$this->db->set_charset(self::MYSQLCHARSET);
if (mysqli_connect_errno())
{
throw new CircleMysqlException("error", 1000);
}
}
/**
* Description:私有化克隆函数,防止外界克隆对象
*/
private function __clone()
{
}
/**
* Description:静态方法,单例访问统一入口
* @return Singleton:返回应用中的唯一对象实例
*/
public static function GetInstance($dbname, $username, $password)
{
$hash = (string) $dbname;
if (!isset(self::$instances[$hash]))
{
self::$instances[$hash] = new self($dbname, $username, $password);
}
return self::$instances[$hash];
}
/**
* Description:获取数据库的私有方法的连接句柄
*/
public function GetDbConnect()
{
return $this->db;
}
/**
* Description:关闭数据库链接的方法
*/
public function CloseDbConnect($dbname)
{
$hash = (string) $dbname;
self::$instances[$hash]->db->close();
unset(self::$instances[$hash]);
}
}