title: PDO class construction in OO PHP
body: <p>Is executing an sql query okay if I'll do it inside a class? Because I do want to use DRY formatting to use execute prepare fetch all the time when executing queries. Is it okay when talking about performance and speed in PHP?</p>
  
  <pre><code>class foo{
  
  protected function dbConnect() 
  {
      $con="mysql:host=yourhost;dbname=yourdbname";
      try{
       $conn=new PDO($con, "user", "password");
       $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   
      }
      catch (PDOexception $e)
      {
      die ("error: ".$e-&gt;getMessage());
      }
      return $conn;
  }
  
  public function selectQuery($query,$fieldsvalues,$type)
      {
          $executequery=$this-&gt;dbConnect()-&gt;prepare($query);
          $executequery-&gt;execute($fieldsvalues);
          if($type=='fetchColumn()')
          {
              return $queryexecute-&gt;fetchColumn();
          }
          if($type=='fetch()')
          {
              return $queryexecute-&gt;fetch();
          }
          if($type=='fetchAll()')
          {
              return $queryexecute-&gt;fetchAll();
          }
      }
  
  public function otherQuery($query,$fieldsvalues)
      {
          $queryexecute=$this-&gt;dbConnect()-&gt;prepare($query);
          $queryexecute-&gt;execute($fieldsvalues);
          return  $queryexecute-&gt;rowCount();
  
      }
  
  }
  
  $newquery=new foo();
  $newquery-&gt;selectQuery("SELECT foo from bar where foobar='?'",array('baz'),'fetchColumn()');
  </code></pre>