title: Pass PDO connection objects by reference?
body: <p>I recently began restructuring some of my "copy-paste" database classes I use for projects. I was working over the login class and came across an interesting function.</p>
  
  <p>My Login class has a function that looks like this:</p>
  
  <pre><code>function &amp;db_connect() {
      require_once('mysql.php');
      $db = new DB();
      return $db;
  }
  </code></pre>
  
  <p>What's interesting is it returns a reference to the <code>$db</code> object instead of the actual object. Thats all fine, but then I came across this:</p>
  
  <pre><code>function __construct(&amp;$db) {
      $this-&gt;db = $db;
      $this-&gt;date = $GLOBALS['date'];
      if ($_SESSION['logged']) {
          $this-&gt;_checkSession();
      }
  }
  </code></pre>
  
  <p>That code is used like this:</p>
  
  <pre><code>$db = db_connect();
  $user = new User($db);
  </code></pre>
  
  <p>As you can see, what's actually going on is that I am passing around a reference to the <code>DB</code> instance rather than the actual object. I obviously had my reasons when I wrote this code 3 years ago, so now I'm wondering, why?</p>
  
  <p>Should all my "DB" classes and objects be passed by reference? Does simply passing by value, say, a PDO object, cause it to create new connections? What's correct practice and what should I be using?</p>