PHP设想形式-DAO (Data Access Objects) 数据接见对象形式

整顿一下本身的进修Aaron Saray 写的PHP设想形式一些demo和本身的明白。大佬看完假如发明鄙人明白有误请马上指出,谢谢拍砖,跪求鞭打

/**
 * DAO (Data Access Objects) 数据接见对象
 * -------------------------------------
 * ** 来自申明 **
 *
 * 数据接见对象设想形式形貌了怎样建立供应通明接见任何数据源的对象
 *
 * 目标是处置惩罚以下两种特定的题目:
 *     1. 反复
 *  2. 数据源笼统化
 *  数据接见对象形式供应数据库笼统层
 *  如今,运用程序的重要处置惩罚代码不再斟酌数据库引擎或表关联
 *  挪用这类对象的大众方法会返回任何数据范例,不必斟酌内涵SQL所需的范例
 * 
 * ===================================== 
 * ** 运用场景 **
 *
 * 数据接见
 * 
 * -------------------------------------
 * 
 * @version ${Id}$
 * @author Shaowei Pu <54268491@qq.com>
 */


abstract class baseDao{
    /**
     * [$_connection 衔接对象]
     * @var [type]
     */
    private $_connection;

    /**
     * [__construct 实例化数据库衔接]
     * @author         Shaowei Pu <pushaowei@sporte.cn>
     * @CreateTime    2017-02-22T17:52:04+0800
     */
    public function __construct(){
        try{        
            $this->_connection = new \PDO("mysql:dbname=mysql;host=localhost","root","pushaowei");
            $this->_connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        }catch(PDOException $e){
            die('error:'.$e->getMessage());
        }   
    }
    /**
     * [feach description]
     * @author         Shaowei Pu <pushaowei@sporte.cn>
     * @CreateTime    2017-02-22T18:01:48+0800
     * @return                              [type] [description]
     */
    public function fetch( $value , $key = ''){
        // SQL START
        $sql = 'SELECT * FROM '.$this->_tablename.' WHERE '.$key.' = "'.$value.'"';
        // 输出
        $dispose = $this->_connection->query($sql);
        return $dispose->fetch(PDO::FETCH_ASSOC);        
    }

}
class selectHandle extends baseDao{
    /**
     * [$_tablename 获得表名]
     * @var string
     */
    protected $_tablename = 'db';

    /**
     * [getValue description]
     * @author         Shaowei Pu <pushaowei@sporte.cn>
     * @CreateTime    2017-02-22T18:06:58+0800
     * @param                               [type] $value [description]
     * @return                              [type]        [description]
     */
    public function getDbValue( $value ){
        $result = parent::fetch( $value, 'Host' );
        return $result;
    }
}

$select = new selectHandle;
var_dump($select->getDbValue('localhost'));

/* 
+----------------------------------------------------------------------
|    array (size=22)
|      'Host' => string 'localhost' (length=9)
|      'Db' => string 'sys' (length=3)
|      'User' => string 'mysql.sys' (length=9)
|      'Select_priv' => string 'N' (length=1)
|      'Insert_priv' => string 'N' (length=1)
|      'Update_priv' => string 'N' (length=1)
|      'Delete_priv' => string 'N' (length=1)
|      'Create_priv' => string 'N' (length=1)
|      'Drop_priv' => string 'N' (length=1)
|      'Grant_priv' => string 'N' (length=1)
|      'References_priv' => string 'N' (length=1)
|      'Index_priv' => string 'N' (length=1)
|      'Alter_priv' => string 'N' (length=1)
|      'Create_tmp_table_priv' => string 'N' (length=1)
|      'Lock_tables_priv' => string 'N' (length=1)
|      'Create_view_priv' => string 'N' (length=1)
|      'Show_view_priv' => string 'N' (length=1)
|      'Create_routine_priv' => string 'N' (length=1)
|      'Alter_routine_priv' => string 'N' (length=1)
|      'Execute_priv' => string 'N' (length=1)
|      'Event_priv' => string 'N' (length=1)
|      'Trigger_priv' => string 'Y' (length=1)
+----------------------------------------------------------------------
*/



    原文作者:无敌小笼包
    原文地址: https://segmentfault.com/a/1190000008459450
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞