52)PHP,加了单例模式的数据库代码

 1 <?php
 2     class db
 3     {
 4         public $host ;//= "localhost";//定义默认连接方式
 5         public $User;//= "root";//定义默认用户名
 6         public $Pwd;//= "root";//定义默认的密码
 7         public $Dbname ;//= "thkphp5";//定义默认的数据库名
 8         public $my_sql;
 9         public $link;
10         public $result;
11        //  protected static $_dbh = null; //静态属性,所有数据库实例共用,避免重复连接数据库,这个是学来的,看的别人的代码,觉得不错,摘过来的
12        /********************************************************************
13        ********************************************************************
14        **         下面的这个就是单利模式所需要的代码           *******
15        *******************************************************************************/
16          public static function instance($config){
17             if(!isset($this->link){
18                 $this->link=new self($config);
19                 //这里还可以这样写:
20                 //$this->link=$this->__construct($config);
21                 //因为实例化对象就是调用一次类的__construct()函数。
22             
23             }
24             return $this->link;
25          
26          }
27          //下面的这个克隆函数变成私有的,这样就能在类的外面克隆了。
28          private function __clone(){}
29          /********************************************************************
30        ********************************************************************
31        **         !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!           *******
32        *******************************************************************************/
33           /*
34          * 构造函数
35          * 主机名,使用者,使用者密码,数据库的名字,查询语句
36          */
37         public function __construct($config) {
38         $this->host=$config['host'];
39         $this->User=$config['user'];
40         $this->Pwd=$config['pwd'];
41         $this->Dbname=$config['dbname'];
42         $this->my_sql=$config['sql'];
43 //这个也是摘过来的的(if)
44         //if ( is_null(self::$_dbh) ) {
45            $this->link= $this->_connect();
46        // }
47       $this->result=  $this->Query($this->my_sql);
48 
49         }
50     
51         //成员方法   是用来执行sql语句的方法
52         /*
53          * 数据库查询函数
54          * $sql   string   是你的查询语句
55          */
56         public function Query($sql)
57             //两个参数:sql语句,判断返回1查询或是增删改的返回
58         {
59             $db = $this->connect();
60             $r = $db->query($sql);
61             if (isset($r)) {
62                 return $r->fetch_all();//查询语句,返回数组.执行sql的返回方式是all,也可以换成row
63             } else {
64                 return "数据库查询失败!";
65             }
66     
67     
68         }
69         /*
70          * 数据库连接函数
71          */
72         public function connect(){
73             $Link= mysqli_connect($this->host,$this->User,$this->Pwd,$this->Dbname);
74           //$this->dbh=
75             return $Link;
76         }
77         
78     }
79     //$sql='select * from zixun;';
80     //$config=include './BBB.php';
81   // $shujuku=new db($config);
82 
83 
84 //  include './login.html';
85 //var_dump($shujuku->result);
86 
87 ?>

注意在实例化具有了单例模式函数的类时,是这样实例化的:                         类名::instance($config);

原文地址:https://www.cnblogs.com/xiaoyoucai/p/7371766.html