title: get_instance() in Codeigniter: Why assign it to a variable?
body: <p>In Codeigniter, <code>get_instance()</code> is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it returns the Controller class instance). I'll include the current source code:</p>
  
  <p><code>get_instance()</code> is defined in <code>Codeigniter.php</code></p>
  
  <pre><code>// Load the base controller class
  require BASEPATH.'core/Controller.php';
  
  function &amp;get_instance()
  {
      return CI_Controller::get_instance();
  }
  </code></pre>
  
  <p>And <code>CI_Controller</code> is defined in <code>Controller.php</code></p>
  
  <pre><code>class CI_Controller {
  
      private static $instance;
  
      /**
       * Constructor
       */
      public function __construct()
      {
          self::$instance =&amp; $this;
  
          // Assign all the class objects that were instantiated by the
          // bootstrap file (CodeIgniter.php) to local class variables
          // so that CI can run as one big super object.
          foreach (is_loaded() as $var =&gt; $class)
          {
              $this-&gt;$var =&amp; load_class($class);
          }
  
          $this-&gt;load =&amp; load_class('Loader', 'core');
  
          $this-&gt;load-&gt;set_base_classes()-&gt;ci_autoloader();
  
          log_message('debug', "Controller Class Initialized");
      }
  
      public static function &amp;get_instance()
      {
          return self::$instance;
      }
  }
  </code></pre>
  
  <p>Here's how it is recommended to be used in the <a href="http://codeigniter.com/user_guide/general/creating_libraries.html" rel="noreferrer">user guide for creating libraries</a>:</p>
  
  <blockquote>
    <h3>Utilizing CodeIgniter Resources within Your Library</h3>
    
    <p>To access CodeIgniter's native resources within your library use the
    <code>get_instance()</code> function. This function returns the CodeIgniter super
    object.</p>
    
    <p>Normally from within your controller functions you will call any of
    the available CodeIgniter functions using the <code>$this</code> construct:
    <code>$this-&gt;load-&gt;helper('url'); $this-&gt;load-&gt;library('session');
    $this-&gt;config-&gt;item('base_url');</code> etc.</p>
    
    <p><code>$this</code>, however, only works directly within your controllers, your
    models, or your views. If you would like to use CodeIgniter's classes
    from within your own custom classes you can do so as follows:</p>
    
    <p>First, assign the CodeIgniter object to a variable:</p>
    
    <p>$CI =&amp; get_instance();</p>
    
    <p>Once you've assigned the object to a variable, you'll use that
    variable instead of <code>$this</code>:
        $CI =&amp; get_instance();
        $CI->load->helper('url'); $CI->load->library('session');
        $CI->config->item('base_url'); etc.</p>
    
    <p>Note: You'll notice that the above <code>get_instance()</code> function is being
    passed by reference:</p>
    
    <p>$CI =&amp; get_instance();</p>
    
    <p><strong>This is very important.</strong> Assigning by reference allows you to use the
    original CodeIgniter object rather than creating a copy of it.</p>
  </blockquote>
  
  <p>Related posts: <a href="https://stackoverflow.com/questions/4740430/explain-ci-get-instance/4740548#4740548">explain $CI =&amp; get_instance();</a> / <a href="https://stackoverflow.com/questions/2819435/codeigniter-get-instance">Codeigniter: Get Instance</a></p>
  
  <p>So, here is my actual question:</p>
  
  <p>Why does the user guide recommend assigning <code>get_instance()</code> to a variable? I'm fairly certain I understand the implications of not assigning by reference, but why is it recommended to assign it to a variable when <code>get_instance()-&gt;load-&gt;model()</code> works fine?</p>
  
  <p>I see a lot of user defined or third party classes in CI that assign to a property of the object:</p>
  
  <pre><code>class MY_Class {
  
      private $CI;
  
      function __construct()
      {
          $this-&gt;CI =&amp; get_instance();
      }
      function my_func()
      {
          $this-&gt;CI-&gt;load-&gt;view('some_view');
      }
      function my_other_func()
      {
          $this-&gt;CI-&gt;load-&gt;model('some_model');
      }
  }
  </code></pre>
  
  <p>Poor example, but I see this frequently. Why bother with this method instead of just calling <code>get_instance()</code> directly? It <em>seems</em> like assigning the entire Controller object to a class variable wouldn't be a great idea, even if it is a reference. Maybe it doesn't matter.</p>
  
  <p>I want to write a wrapper function for <code>get_instance()</code> so it's easier to type, and I don't have to constantly assign it to a variable.</p>
  
  <pre><code>function CI()
  {
      return get_instance();
  }
  </code></pre>
  
  <p>Or:</p>
  
  <pre><code>function CI()
  {
      $CI =&amp; get_instance();
      return $CI;
  }
  </code></pre>
  
  <p>Then I could use <code>CI()-&gt;class-&gt;method()</code> from anywhere without the hassle of assigning it to a variable, it's very easy to write and understand what it does, and can result in shorter, more elegant code.</p>
  
  <ul>
  <li>Is there any reason not to take this approach?</li>
  <li>Is there any difference between the two <code>CI()</code> functions above?</li>
  <li>Why is it recommended to assign <code>get_instance()</code> to a variable rather than calling it directly?</li>
  <li>What does the <code>&amp;</code> in <code>function &amp;get_instance(){}</code> mean where it is defined? I know a bit about what <a href="http://www.php.net/manual/en/language.references.php" rel="noreferrer">references</a> are for and I use them when appropriate, but I've never seen a function defined this way. If I do write a wrapper function, should I use this as well?</li>
  </ul>
  
  <p>Please note that this is not so much a <strong>style</strong> question, but a technical one. I want to know if there are any issues, performance <em>or otherwise</em>, with using the method I'm suggesting.</p>
  
  <p><strong>EDIT</strong>: So far we have:</p>
  
  <ul>
  <li>Method chaining is not available in php4, so assigning to a variable is a workaround (although this is fairly irrelevant as Codeigniter has dropped php4 support)</li>
  <li>The minor overhead of calling a function more than once to return the object, rather than calling it once and assigning to a variable.</li>
  </ul>
  
  <p>Anything else, or are these the only potential issues?</p>