PHP design pattern – DAO (Data Access Objects) data access object pattern

Time:2022-8-13

Organize your studiesAaron SarayWrite some demos and own understanding of PHP design patterns. After reading it, if you find that my understanding is wrong, please point it out immediately, thank you for Paizhuan, and beg for whipping

/**
 * DAO (Data Access Objects) data access objects
 * -------------------------------------
 ** From description**
 *
 * The Data Access Object design pattern describes how to create objects that provide transparent access to any data source
 *
 * The purpose is to solve the following two specific problems:
 * 1. Repeat
 * 2. Data source abstraction
 * Data Access Object Pattern provides database abstraction layer
 * Now, the main processing code of the application no longer considers the database engine or table relationships
 * Calling a public method of such an object will return any data type, regardless of the type required by the intrinsic SQL
 * 
 * ===================================== 
 * ** Application scenarios**
 *
 * data access
 * 
 * -------------------------------------
 * 
 * @version ${Id}$
 * @author Shaowei Pu <[email protected]>
 */
abstract class baseDao{
    /**
     * [$_connection connection object]
     * @var [type]
     */
    private $_connection;

    /**
     * [__construct instantiate database connection]
     * @author         Shaowei Pu <[email protected]>
     * @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 <[email protected]>
     * @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.'"';
        // output
        $dispose = $this->_connection->query($sql);
        return $dispose->fetch(PDO::FETCH_ASSOC);        
    }

}
class selectHandle extends baseDao{
    /**
     * [$_tablename get table name]
     * @var string
     */
    protected $_tablename = 'db';

    /**
     * [getValue description]
     * @author         Shaowei Pu <[email protected]>
     * @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)
+----------------------------------------------------------------------
*/