zoukankan      html  css  js  c++  java
  • 数据访问对象模式

    数据访问对象模式


    该设计模式描述了如何创建提供透明访问任何数据源的对象.

    // 这是一个单利模式用于提供mysql pdo链接
    class MysqlConnect
    {
        protected $link = null;
        protected static $instance = null;
    
        public static function getInstance()
        {
            if (is_null(self::$instance)) {
                self::$instance = new MysqlConnect();
            }
            return self::$instance;
        }
    
        protected function __construct()
        {
            $this->connect();
        }
    
        public function connect()
        {
            if (is_null($this->link)) {
                $this->link = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root');
            }
        }
    
        public function close()
        {
            $this->link = null;
        }
    
        public function getLink()
        {
            return $this->link;
        }
    }
    
    abstract class DAO
    {
    
        protected $table = null;
        protected $link = null;
    
        public function __construct()
        {
            $this->link = MysqlConnect::getInstance()->getLink();
        }
    
    
    
        public function fetch()
        {
            $res = $this->link->query("SELECT * FROM {$this->table}");
            $data = array();
    
            foreach ($res as $row) {
                $data[] = $row;
            }
            return $data;
        }
    }
    
    class CD extends DAO
    {
        protected $table = 'cd';
    }
    
    $cd = new CD();
    var_dump($cd->fetch());
    

      

  • 相关阅读:
    原生小程序音频播放
    Vue定义全局过滤器filter
    系统扩展性之引入外部包
    oracle update join
    OAuth2
    oracle pl/sql
    MySQL同步工具otter的使用介绍(一)
    python批量安装apk
    mac brew安装redis
    antd 修改Modal的底部按钮颜色
  • 原文地址:https://www.cnblogs.com/itfenqing/p/7750599.html
Copyright © 2011-2022 走看看