• 0606-工厂模式、单例模式、DBDA的单例和完整功能


    工厂模式:只要指定类名,就可以据此获取一个该类的对象。

    单例模式:某个类,只允许其“创建”出一个对象。

    单例的方法:三私一公(一个私有化对象,一个私有化构造方法,一个私有化克隆方法,一个公共方法返回对象)。

    DBDA类的单例模式和完整功能:

      1 <?php
      2 //$db = new mysqli('localhost:3306','root','','per_position');
      3 echo '<meta charset="utf-8">';
      4 class DBDA{
      5     //定义数据库链接参数
      6     public $host = 'localhost';
      7     public $user = 'root';
      8     public $pwd = '';
      9     public $database = 'per_position';
     10     public $charset = 'utf8';
     11     
     12     //数据库连接对象
     13     public $db = null;
     14     //DBDA对象
     15     private static $link = null;
     16     
     17     //禁止构造:初始化数据
     18     private function __construct($configArr=array()){
     19         $this->host = isset($configArr['host']) ? $configArr['host'] : 'localhost';
     20         $this->user = isset($configArr['user']) ? $configArr['user'] : 'root';
     21         $this->pwd = isset($configArr['pwd']) ? $configArr['pwd'] : '';
     22         $this->database = isset($configArr['database']) ? $configArr['database'] : 'per_position';
     23         $this->charset = isset($configArr['charset']) ? $configArr['charset'] : 'utf8';
     24         //链接数据库
     25         $this->connect();
     26     }
     27     //禁止克隆
     28     private function __clone(){}
     29     //提供公有方法返回对象
     30     static function getDb($config=array()){
     31         if(!isset(self::$link)){
     32             self::$link = new self($config);
     33         }
     34         return self::$link;
     35     }
     36     //链接数据库的方法
     37     function connect(){
     38         $this->db = new mysqli($this->host,$this->user,$this->pwd,$this->database);
     39         !mysqli_connect_error() or die('连接失败');
     40         $this->db->query('set names '.$this->charset);
     41     }
     42     //执行sql语句的方法
     43     function query($sql){
     44         $res = $this->db->query($sql);
     45         if(!$res){
     46             echo ("<br />执行失败。");
     47             echo "<br />失败的sql语句为:" . $sql;
     48             echo "<br />出错信息为:" . mysqli_error($this->db);
     49             echo "<br />错误代号为:" . mysqli_errno($this->db);
     50             die();
     51         }
     52         return $res;
     53     }
     54     //返回二维数组
     55     function getAll($sql){
     56         $res = $this->query($sql);
     57         return $res->fetch_all();
     58     }
     59     //返回字符串
     60     function getStr($sql){
     61         $res = $this->query($sql);
     62         $arr = $res->fetch_all();
     63         $str = '';
     64         foreach($arr as $v){
     65             foreach($v as $vv){
     66                 $str .= $vv.",";
     67             }
     68             $str = substr($str,0,-1);
     69             $str .= "^";
     70         }
     71         $str =  substr($str,0,-1);
     72         return $str;
     73     }
     74     //返回json
     75     function getJson($sql){
     76         $res = $this->query($sql);
     77         $arr = $res->fetch_all();
     78         return json_encode($arr);
     79     }
     80     //返回关联数组
     81     function getAssoc($sql){
     82         $res = $this->query($sql);
     83         $arr = array();
     84         while($row = $res->fetch_assoc()){
     85             $arr[$row['department']]=$row;
     86         }
     87         return $arr;
     88     }
     89 }
     90 //$config = array(
     91 //    //'database' => 'mysql'
     92 //);
     93 //$DBDA = new DBDA();
     94 //var_dump($DBDA->getJson('select * from user'));
     95 //看错误提示
     96 //$DBDA->query('select * from 123');
     97 //$res = $DBDA->db->query('select * from user');
     98 //var_dump($res->fetch_all());
     99 
    100 
    101 
    102 //禁用了构造方法,直接new会报错。
    103 //$DBDA = DBDA::getDb();
    104 //var_dump($DBDA);
    105 //echo "<br>";
    106 //$DBDA = DBDA::getDb();
    107 //var_dump($DBDA);
    DBDA类的单例模式和完整功能
    1 <?php
    2 include('DBDA.class.php');
    3 $dbda = DBDA::getDb();
    4 var_dump($dbda->getAll('select * from user'));
    类的引用
  • 相关阅读:
    Different ways how to escape an XML string in C#
    __VIEWSTATE
    Git for Computer Scientists
    关于SQL Server死锁
    20个开源项目托管站点
    Understanding Host Headers in IIS
    开发与研发:区别很大
    Linux 0.12 “轮子”任务调度图示
    RabbitMQ学习第二记:工作队列的两种分发方式,轮询分发(Roundrobin)和 公平分发(Fair dispatch)
    ResultSetMetaData中getColumnLabel和getColumnName的区别
  • 原文地址:https://www.cnblogs.com/flypea93/p/9149156.html
Copyright © 2020-2023  润新知