title: Singleton v Single Instance DB Connection in PHP
body: <p>I'm moving onto teaching myself OOP in PHP.</p>
  
  <p>I'm creating a couple of little web apps and have followed a lot of tutorials that either create the database (using PDO) via a Singleton, or via passing the global around. I've read that these are pretty much the same thing and are both to be avoided like the plague.</p>
  
  <p>So I've watched the Google Tech Talks on clean code, and read almost every SO article on dependency injection and the like. I have a couple of questions.</p>
  
  <ol>
  <li>The clean code videos suggest you shouldn't do 'work' in your constructors. Is this 'work' in reference to business logic. Ie. If my class's job is to create another object, is that an OK kind of 'work'?</li>
  </ol>
  
  <p>For example, in trying to conform to single repsonibility classes I created three.</p>
  
  <ol>
  <li>Class DB - which actually connects to the database.</li>
  <li>Class DBFactory - which creates the DB object which connects to the database.</li>
  <li>Class DBInstance - which returns a single instance of the DBFactory created PDO object.</li>
  </ol>
  
  <p>Please note that I'm trying to create a single instance, without creating a Singleton pattern.</p>
  
  <p>So I try and pass my dependencies for each class up the chain. I find myself in a position where I have to create all of the objects (from DB down) so I can inject the dependencies. For some reason I thought it would work the other way, I'd create the first object, which would create the second for me etc. I'm clearly missing something?</p>
  
  <p>Hopefully this helps others as well - there seems to be a myriad of questions relating to this stuff and databases but very little good examples.</p>
  
  <p>(I should mention this does work, I do get a list of hotel names out of the database!)</p>
  
  <p>TestCode.php</p>
  
  <pre><code>include './classes/DB.php';
  include './classes/DBFactory.php';
  include './classes/DBInstance.php';
  include './classes/Location.php';
  
  $db = new DB;
  $dbfactory = new DBFactory($db);
  $dbinstance = new DBInstance($dbfactory);
  $dbh = $dbinstance-&gt;getDbInstance();
  
  //Example business logic
  $location_names = Location::getLocationNames($dbh);
  print_r($location_names);
  </code></pre>
  
  <p>Class DB.php:</p>
  
  <pre><code>class DB {
  
      private $_dbhost = 'myhost';
      private $_dbname = 'myname';
      private $_dbuser = 'myuser';
      private $_dbpass = 'mypass';
  
      private $_error;
  
      public function connect() {
          try {
              return new PDO("mysql:host=$this-&gt;_dbhost;dbname=$this-&gt;_dbname", 
                  $this-&gt;_dbuser, $this-&gt;_dbpass);
          }
  
          catch (PDOException $e) {
              $this-&gt;_error = 'Error! ' . $e-&gt;getMessage() . '&lt;br /&gt;';
              die();
          }
      }
  
      public function getError() {
          if (isset($this-&gt;_error)) {
              return $this-&gt;_error;
          }
      }
  }
  </code></pre>
  
  <p>Class DBFactory.php</p>
  
  <pre><code>class DBFactory {
  
      private $_dbh;
  
      public function __construct(DB $db) {
          $this-&gt;_dbh = $db;
      }
  
      public function Create() {
          return $this-&gt;_dbh-&gt;Connect();
      }
  }
  </code></pre>
  
  <p>Class DBInstance.php</p>
  
  <pre><code>class DBInstance {
  
      private static $_dbinstance;
  
      public function __construct(DBFactory $dbfactory) {
  
          if (!isset(self::$_dbinstance)) {
              self::$_dbinstance = $dbfactory-&gt;Create();
          }
      }
  
      public function getDbInstance() {
          return self::$_dbinstance;
      }
  }
  </code></pre>