PHP設計模式-DAO (Data Access Objects) 資料訪問物件模式

無敵小籠包發表於2019-05-14

整理一下自己的學習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)
+----------------------------------------------------------------------
*/



相關文章