title: PDO with Singleton cannot access private property
body: <p>I´ve made a database connection with PDO and the singleton pattern.
  Everything works fine as long as I have the <code>$_db</code> variable set as public but it needs to be private... When I make it private I off course get the <strong>Error: Cannot access private property Database::$_db</strong>
  Can someone tell me how to make it private and still be able to get an instance?</p>
  
  <p>When I call the database connection from another file I call the function <code>getInstance()</code>
  Here is an example how I call it from one file:</p>
  
  <pre><code>$db = Database::getInstance();
  $query = $db-&gt;_db-&gt;prepare("SELECT `password`, `id` FROM `users` WHERE `username` = ?");     
  </code></pre>
  
  <p>This is what my database connection file looks like:</p>
  
  <pre><code>class Database 
  {
      private $_db;
      static $_instance;
  
      private function __construct() 
      {
          $this-&gt;_db = new PDO('mysql:host=localhost;dbname=mvcuser', 'root', '');
          $this-&gt;_db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      }
  
      private function __clone(){}
  
      public static function getInstance() 
      {
          if (!(self::$_instance instanceof self)) 
          {
              self::$_instance = new self();
          }
          return self::$_instance;
      }
  
      public function query($sql) 
      {
      return $this-&gt;_db-&gt;query($sql);
      }
  }
  </code></pre>