title: Singleton Pattern for Database Connection in PHP?
body: <p>I read many posts and blogs about Singleton and database connection. What I understand from the singleton definition is that you can use it on any class object that is instantiated many times while staying intact. 
  I have a web application, and I was wondering if I could implement Singleton for database connection</p>
  
  <p>Here is what I have under the DatabaseGateway class:</p>
  
  <pre><code>class DatabaseGateway
  {
      private $DBConnection;
      private $serverName;
      private $userName;
      private $password;
      private $databaseName;
  
      public function __construct() {
         //not done
      }
  
      public function getDBConnection() {
          return $this-&gt;DBConnection;
      }
  
      public function openDBConnection() {
          $this-&gt;DBConnection = new mysqli($this-&gt;serverName, $this-&gt;userName, $this-&gt;password, $this-&gt;databaseName);
      }
  
      public function closeDBConnection() {
          mysqli_close($this-&gt;DBConnection);
      }
  </code></pre>
  
  <p>The method <strong>OpenDBConnection</strong> is used when users log in in the UserGateway class. I was wondering if it is a good idea, and if it possible, to make openDBconnection into a singleton, while everyone has their own account and password.</p>