zoukankan      html  css  js  c++  java
  • PHP单例模式实例,连接数据库对类的引用

    <?php
    //单例模式连接数据库
    class pzhang{
    static private $instance;
    private static $config;
    private $dbase = array(
    'host' => 'localhost',
    'username' =>'root',
    'password'=>'root',
    'dbname' =>'jmyp'
    );
    private function __construct(){
    }
    static public function getInstance(){
    if(!self::$instance instanceof self)
    self::$instance = new self();
    return self::$instance;
    }
    public function conn(){
    $mysql_db = $this->dbase['dbname'];
    self::$config = new mysqli($this->dbase['host'],$this->dbase['username'],$this->dbase['password']);
    self::$config->query('set name utf8');
    self::$config -> select_db($mysql_db);
    $sql = "select * from admin";
    $row = self::$config->query($sql);
    $data = [];
    while($tmp = $row->fetch_assoc()){
    $data[] = $tmp;
    }
    echo "<pre>";
    print_r($data);
    echo "</pre>";
    return self::$config;
    }
    }
    $obj = pzhang::getInstance();
    $obj->conn();

    //单例模式对类的引用
    class zhangp{
    public function system(){
    echo "learning more";
    }
    }

    class singleCase{
    static private $instance;
    private $avg;
    private function __construct($config){
    self::$instance = new $config;
    }
    static public function getInstance($avg){
    if(!self::$instance instanceof self)
    new self($avg);
    return self::$instance;
    }
    }
    $obj = singleCase::getInstance('zhangp');
    $obj->system();
    ?>
  • 相关阅读:
    IBatisNet不常用到的配置(Dao.config ConnectionTimeout),居然不起作用(前辈留给我们的坑)
    随机数 字母 数字
    证书文件(pfx)读取时报 “指定的网络密码不正确”
    SQL多结果集导出Excel
    Uva514
    PAT乙级1012
    栈-41
    位运算-89
    PAT乙级1028
    PAT乙级1029
  • 原文地址:https://www.cnblogs.com/isuansuan/p/9759535.html
Copyright © 2011-2022 走看看