title: Trying to create a database connection with PDO and singleton pattern
body: <p>I've been trying to create a database connection with PDO and singleton. I think I'm pretty much finished but I'm not sure if I'm using a correct singleton pattern. </p>
  
  <p>I'm also not sure if I'm using the <code>__clone()</code> and the <code>__wakeup()</code> methods correctly. And I don't have the knowledge to test them.</p>
  
  <p>Could anyone please tell me if my approach is on the right path and if I'm using the singleton pattern correctly? I'm very new to design patterns.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;?php
  
  require_once 'config.php';
  
  class dbConn{
  
  // Variable to store connection object.
  protected static $db;
  
  // Assign variables from config.php.
  private $host   = DB_HOST;
  private $dbuser = DB_USER;
  private $dbpass = DB_PASS;
  private $dbname = DB_NAME;
  
  // Private construct - class cannot be instatiated externally.
  private function __construct() {
  
      try {
          // Try to create PDO object to the $db variable.
          $pre = 'mysql:host=' . $this-&gt;host . ';dbname=' . $this-&gt;dbname;
          self::$db = new PDO($pre, $this-&gt;dbuser, $this-&gt;dbpass);
          self::$db-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
      }
      // If not able to connect to database.
      catch (PDOException $e) {
          echo "Could not connect: " . $e-&gt;getMessage();
      } 
  }
  
  // Get connection function.
  public static function getConnection() {
  
  // Only if no connection object exists it creates one, because we only want one instance.
      if (!self::$db) {
  // New connection object.
          new dbConn();
  }
      return self::$db;
  }
  
  public function __clone() {
          return false;
      }
  
  public function __wakeup(){
          return false;
      }
  }
  
  $db = dbConn::getConnection()
  
  ?&gt;
  </code></pre>