单例模式 DB

/**
 *链接数据库的静态类方法
 */
class Db{
public $host;
public $user;
public $pwd;
static private $sign;
private function __clone(){ 
die("不能实例化此静态类");
}
private function __construct($host,$user,$pwd){
$this->host = $host;
$this->user = $user;
$this->pwd = $pwd;
mysql_connect($this->host,$this->user,$this->pwd) or die("connect is error");
}
public static function getdb($host,$user,$pwd){
if(is_null(self::$sign)){//die("asdf");
self::$sign = new Db($host,$user,$pwd);
}
return self::$sign;

}
static public function set_table($db){
return mysql_select_db($db);
}
static public function query($sql){
return mysql_query($sql);
}

$link = Db::getdb("127.0.0.1","root","");
$db = Db::set_table("test");
$data = Db::query("select *from shops");
var_dump($data);

你可能感兴趣的:(php,PHP高级篇,设计模式)