explain $CI =& get_instance();

PhpCodeigniter

Php Problem Overview


Looking through codeigniter's source code,

in its helper functions I keep seeing code $CI =& get_instance(); can anyone please explain to me how this code works?

I get that it is returning a reference to the $CI super object, but where does get_instance() come from?

Php Solutions


Solution 1 - Php

It's basically a Singleton Design Pattern that uses a function instead of a static method.

To look deeper, check out the source code

So basically, it doesn't enforce the singleton, but it's a shortcut to a public function...

Edit: Actually, now I understand. For PHP4 compatibility they had to do a double-global-variable-hack to get it to return the references properly. Otherwise the references would get all screwed up. And since PHP4 didn't have support for static methods (well, properly anyway), using the function was the better way. So it still exists for legacy reasons...

So if your app is PHP5 only, there should be nothing wrong with doing CI_Base::get_instance(); instead, it's identical...

Solution 2 - Php

get_instance() is a function defined in the core files of CodeIgniter. You use it to get the singleton reference to the CodeIgniter super object when you are in a scope outside of the super object.

I'm pretty sure it's defined in base.php or something similar.

Solution 3 - Php

Only the class that extends CI_Controller,Model,View can use

$this->load->library('something');
$this->load->helper('something');//..etc

Your Custom Class cannot use the above code. To use the above features in your custom class, your must use

$CI=&get instance();
$CI->load->library('something');
$CI->load->helper('something');

for example,in your custom class

// this following code will not work
Class Car
{
   $this->load->library('something');
   $this->load->helper('something');
}

//this will work
Class Car
{
   $CI=&get_instance();
   $CI->load->library('something');
   $CI->load->helper('something');
}
// Here $CI is a variable.

Solution 4 - Php

this is a singleton structure to understand how the codeigniter loads the libraries and classes

<?php

/*
====================================
start of the loader class
====================================
*/
class Loader {


  protected function _init_class($class){
    $C = Controller::get_instance();
    $name = strtolower($class);
    $C->$name = new $class();
  }

  public function _class($library){
    if(is_array($library)){
      foreach($library as $class){
        $this->library($class);
      }
      return;
    }

    if($library == ''){
      return false;
    }

    $this->_init_class($library);
  }

  public function view ($param) {
     echo $param;
  }
}
/*
===============================
 End of the loader class
==============================
*/

/*
===============================
 start of core controller class
==============================
*/

class Controller {

  private static  $instance;

  function __construct () {
    self::$instance = $this;
    $this->load = new Loader();
  }


  public static function get_instance(){
    return self::$instance;
  }
}
/*
===============================
end of the core controller class
=================================== 
*/

/*
 ====================================================
  start of library sections (put all your library classes in this section)
=====================================================
*/

class MyLibrary {

 private $c;

 function __construct() {
   $this->c = Controller::get_instance();
 }

 function say($sentence) {
   $this->c->load->view($sentence);
 }
}
/*
 ====================================================
  End of the library sections
====================================================
*/

/*
 ============================================
  start of controller section (put all your controller classes in this section)
 ===========================================
*/

class Foo extends Controller {

  function __construct () {
    parent::__construct();
    $this->load->_class('MyLibrary');
  }

  function bar() {
    $this->mylibrary->say('Hello World');
  }
}


/* 
 ==========================================
  End of the controller sections
 ==========================================
*/

$foo = new Foo();
$foo->bar();

Solution 5 - Php

$CI = get_instance(); is to replace $this to $CI on helper,

Solution 6 - Php

Putting it in the constructor worked for me:

function __construct()
{
$this->CI =& get_instance();
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHailwoodView Question on Stackoverflow
Solution 1 - PhpircmaxellView Answer on Stackoverflow
Solution 2 - PhpWadeView Answer on Stackoverflow
Solution 3 - PhpKhant Wai KyawView Answer on Stackoverflow
Solution 4 - Phpisnvi23h4View Answer on Stackoverflow
Solution 5 - PhpTofanView Answer on Stackoverflow
Solution 6 - PhpTejpal SharmaView Answer on Stackoverflow