title: Use PDO database class without creating new connection every time?
body: <p>I got this PDO database class</p>
  
  <pre><code>class clsDatabase{
    // db settings
    private $host   = 'localhost';
    private $user   = 'test';
    private $dbname = 'test';
    private $pass   = 'test1';
  
    private $dbh;
    private $error;
  
    public function __construct(){
          // Set DSN
          $dsn = 'mysql: host=' . $this-&gt;host . ';dbname=' . $this-&gt;dbname;
          // Set options
          $options = array(
              PDO::ATTR_PERSISTENT            =&gt; true,
              PDO::ATTR_ERRMODE               =&gt; PDO::ERRMODE_EXCEPTION,
              PDO::MYSQL_ATTR_INIT_COMMAND    =&gt; 'SET NAMES UTF8'
          );
          // Create a new PDO instanace
          try{
              $this-&gt;dbh = new PDO($dsn, $this-&gt;user, $this-&gt;pass, $options); 
          }
          // Catch any errors
          catch(PDOException $e){
              $this-&gt;error = $e-&gt;getMessage();
              echo $this-&gt;error;
              exit;
          }       
      }
  
      public function query($query){
          $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query);
      }
  }   
  </code></pre>
  
  <p>I try to seperate my code in different classes, for example i got a clsDBUser which is connected to the clsUserController. I do this so i know what class uses what database code. My clsDBUser class looks like this</p>
  
  <pre><code>class clsDBUser extends clsDatabase {
      // construct
      public function __construct() {
          parent::__construct();
      }
  
      // get users
      public function getUsers($users_id){
          $query = "
              SELECT 
                  email
              FROM 
                  users
              WHERE 
                 users_id = :users_id
          ";          
          $this-&gt;query($query);
          $this-&gt;bind(':users_id', $users_id);
  
          if($row = $this-&gt;single()){
              $this-&gt;close();
              return $row;
          }
          $this-&gt;close();
          return false;       
      }
  }
  </code></pre>
  
  <p>I am wondering if this is the way to go or am i creating a new database connection in every class right now? Because normally in PHP4 (yes i know old) i can't recognize i had to make a new database connection every time. </p>
  
  <p>Do i need to improve this, how do i need to improve this?</p>