title: Cloning CodeIgniter's database object
body: <p>I am working on a subquery library for CodeIgniter (<a href="http://labs.nticompassinc.com/CodeIgniter-Subqueries/" rel="nofollow">link to project</a>), which works by returning database objects that you can use instead of the normal <code>$this-&gt;db</code> object.</p>
  
  <p>Originally, I was making new database objects each time:</p>
  
  <pre><code>$this-&gt;CI =&amp; get_instance();
  $db = $this-&gt;CI-&gt;load-&gt;database('', true);
  </code></pre>
  
  <p>But I then realized (or rather someone pointed out to me) that this would make a new database connection each time!  So I decided to try to <code>clone</code> objects instead of making new ones.</p>
  
  <p>I updated the code to look like so (<a href="https://github.com/NTICompass/CodeIgniter-Subqueries/blob/v3.0-beta/libraries/Subquery.php#L56" rel="nofollow">link to full code</a>):</p>
  
  <pre><code>class Subquery{
      var $CI, $db;
  
      function __construct(){
          $this-&gt;CI =&amp; get_instance();
          $this-&gt;db = $this-&gt;CI-&gt;db; // Default database connection
      }
  
      function start_subquery(){
          $newDatabase = clone $this-&gt;db;
          // some more code
          return $newDatabase;
      }
  }
  </code></pre>
  
  <p>I tested this, and it seems to work, but I'm not sure if this solves the problem.  Does using <code>clone</code> make a new database connection, or does it use references internally and only keep one connection?</p>