explain $CI =& get_instance();

前端 未结 5 2056
长情又很酷
长情又很酷 2020-11-30 19:15

Looking through codeigniter\'s source code,

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

相关标签:
5条回答
  • 2020-11-30 19:42

    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...

    0 讨论(0)
  • 2020-11-30 19:42

    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.

    0 讨论(0)
  • 2020-11-30 19:45

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

    0 讨论(0)
  • 2020-11-30 19:57

    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();
    
    0 讨论(0)
  • 2020-11-30 20:04

    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.
    
    0 讨论(0)
提交回复
热议问题