title: Global or Singleton for database connection?
body: <p>What is the benefit of using singleton instead of global for database connections in PHP? I feel using singleton instead of global makes the code unnecessarily complex.</p>
  
  <h2>Code with Global</h2>
  
  <pre><code>$conn = new PDO(...);
  
  function getSomething()
  {
      global $conn;
      .
      .
      .
  }
  </code></pre>
  
  <h2>Code with Singleton</h2>
  
  <pre><code>class DB_Instance
  {
      private static $db;
  
      public static function getDBO()
      {
          if (!self::$db)
              self::$db = new PDO(...);
  
          return self::$db;
      }
  }
  
  function getSomething()
  {
      $conn = DB_Instance::getDBO();
      .
      .
      .
  }
  </code></pre>
  
  <p>If there's a better way of initializing database connection other than global or singleton, please mention it and describe the advantages it have over global or singleton.</p>