<?php
//创建单例的数据库类(工具类)
class Db{
//私有的静态的对象
private static $obj = NULL;
private $db_host;
private $db_user;
private $db_psw;
private $db_name;
private $charset;
//私有的构造方法,防止类外new对象
private function __construct(){
$this->db_host = $GLOBALS['config']['db_host'];
$this->db_user = $GLOBALS['config']['db_user'];
$this->db_psw = $GLOBALS['config']['db_psw'];
$this->db_name = $GLOBALS['config']['db_name'];
$this->connectDb();
$this->selectDb();
$this->setCharset();
}
//私有的克隆方法
private function __clone(){}
//公共的静态的创建对象的方法
public static function getInstance(){
if (!self::$obj instanceof self) {
self::$obj = new self();
}
return self::$obj;
}
//创建私有的连接数据库的方法
private function connectDb(){
if (!@mysql_connect($this->db_host,$this->db_user,$this->db_psw)) {
die(mysql_error());
}
}
//私有的选择数据库的方法
private functio
php创建单例的数据库类(工具类)
最新推荐文章于 2023-04-24 22:29:17 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)