title: PHP: use a class to establish and return a PDO handle
body: <p>To avoid another deprecation problem, I want to use a class to connect to my database and have that class return the database handle, for easy use.</p>
  
  <p>To be a bit more specific, I want to have a class in order to:</p>
  
  <ul>
  <li>Immediately connect to a database.</li>
  <li>To connect as admin or read-only access, depending on the situation.</li>
  <li>Have $db to become $dbh through the use of this class</li>
  <li>Thus, $db becomes an open PDO connection (database handle) -so that I can do $db->query...etc. Hence 'return $this->dbh;'</li>
  </ul>
  
  <p>I tried this approach, but something is not working:</p>
  
  <pre><code>&lt;?php
  
  class database {
      private $dbh;
  
      public function __construct($type=null) {
          switch ($type) {
              case 0:
                  self::ro();
                  break;
              case 1:
                  self::admin();
                  break;
              default:
                  self::ro();
                  break;
              }
          return $this-&gt;dbh;
  }
  
  
      private function ro() {
          try {
              # MySQL with PDO_MYSQL (Read-only access)
              $dbhost = 'localhost';
              $dbname = 'dbname';
              $dbuser = 'dbuser';
              $dbpass = 'dbpass';
              $this-&gt;dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
          }
          catch(PDOException $e) {
              echo $e-&gt;getMessage();
          }  
      }
  
  
      private function admin() {
          try {
              # MySQL with PDO_MYSQL
              $dbhost = 'localhost ';
              $dbname = 'dbname';
              $dbuser = 'dbadminuser';
              $dbpass = 'dbadminpass';
              $this-&gt;dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
          }
          catch(PDOException $e) {
              echo $e-&gt;getMessage();
          }  
      }
  
  }
  
  $db = new database();
  //test connection
  $attributes = array(
      "AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION", "CONNECTION_STATUS",
      "ORACLE_NULLS", "PERSISTENT", "PREFETCH", "SERVER_INFO", "SERVER_VERSION",
      "TIMEOUT"
  );
  
  foreach ($attributes as $val) {
      echo "PDO::ATTR_$val: ";
      echo $db-&gt;getAttribute(constant("PDO::ATTR_$val")) . "&lt;br&gt;\n";
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>Fatal error: Call to undefined method database::getAttribute() in...</p>
  </blockquote>