title: PHP Data Access Object
body: <p>I'm trying to figure out if I'm using the DAO pattern correctly and, more specifically, how abstract db persistence should be by the time it gets to my mapper classes. I'm using PDO as the data-access abstraction object, but sometimes I wonder if I'm trying to abstract the queries too much.</p>
  <p>I've just included how I'm abstracting select queries, but I've written methods for all of the CRUD operations.</p>
  <pre><code>class DaoPDO {
  
      function __construct() {
      
          // connection settings
          $this-&gt;db_host   = '';
          $this-&gt;db_user   = ''; 
          $this-&gt;db_pass   = ''; 
          $this-&gt;db_name   = '';
          
          
      }
  
      function __destruct() {
      
          // close connections when the object is destroyed
          $this-&gt;dbh = null;
      
      } 
      
  
      function db_connect() {
      
          try { 
          
              /**
               * connects to the database -
               * the last line makes a persistent connection, which
               * caches the connection instead of closing it 
               */
              $dbh = new PDO(&quot;mysql:host=$this-&gt;db_host;dbname=$this-&gt;db_name&quot;, 
                              $this-&gt;db_user, $this-&gt;db_pass, 
                              array(PDO::ATTR_PERSISTENT =&gt; true));
  
              
              return $dbh;
          
          } catch (PDOException $e) {
          
              // eventually write this to a file
              print &quot;Error!: &quot; . $e-&gt;getMessage() . &quot;&lt;br/&gt;&quot;;
              die();
          
          }
  
      
      } // end db_connect()'
  
  
      
      function select($table, array $columns, array $where = array(1=&gt;1), $select_multiple = false) {
      
          // connect to db
          $dbh = $this-&gt;db_connect();
          
          $where_columns  = array();
          $where_values   = array();
          
          foreach($where as $col =&gt; $val) {
          
              $col = &quot;$col = ?&quot;;
          
              array_push($where_columns, $col);
              array_push($where_values, $val);
          
          }
          
          
          // comma separated list
          $columns = implode(&quot;,&quot;, $columns);
  
          // does not currently support 'OR' arguments
          $where_columns = implode(' AND ', $where_columns);
          
  
          
          $stmt = $dbh-&gt;prepare(&quot;SELECT $columns
                                 FROM   $table
                                 WHERE  $where_columns&quot;);
                                 
                      
          $stmt-&gt;execute($where_values);
          
          if (!$select_multiple) {
          
              $result = $stmt-&gt;fetch(PDO::FETCH_OBJ);
              return $result;
          
          } else {
          
              $results = array();
          
              while ($row = $stmt-&gt;fetch(PDO::FETCH_OBJ)) {
              
                  array_push($results, $row);
              
              }
              
              return $results;
          
          }
  
              
      
      } // end select()
  
      
  } // end class
  </code></pre>
  <p>So, my two questions:</p>
  <ol>
  <li><p>Is this the correct use of a DAO, or am I misinterpreting its purpose?</p>
  </li>
  <li><p>Is abstracting the query process to this degree unnecessary, or even uncommon? Sometimes I feel like I'm trying to make things too easy...</p>
  </li>
  </ol>