【问题标题】:explain $CI =& get_instance();解释 $CI =& get_instance();
【发布时间】:2011-06-12 01:03:21
【问题描述】:

浏览codeigniter的源码,

在它的辅助函数中,我不断看到代码 $CI =& get_instance(); 谁能给我解释一下这段代码是如何工作的?

我知道它正在返回对 $CI 超级对象的引用,但 get_instance() 来自哪里?

【问题讨论】:

标签: php codeigniter


【解决方案1】:

它基本上是一个Singleton Design Pattern,它使用函数而不是静态方法。

要深入了解,请查看source code

所以基本上,它不会强制执行单例,但它是公共函数的快捷方式...

编辑:其实,现在我明白了。为了与 PHP4 兼容,他们必须执行 double-global-variable-hack 才能使其正确返回引用。否则参考文献会搞砸的。而且由于 PHP4 不支持静态方法(好吧,无论如何都是正确的),使用该函数是更好的方法。所以由于遗留原因它仍然存在......

因此,如果您的应用程序仅是 PHP5,那么 应该 使用 CI_Base::get_instance(); 并没有什么问题,它是相同的......

【讨论】:

  • 何时使用 CI 超级对象,为什么?你能指出一些关于 CI 超级对象的 CI 文档吗?
  • +1 实际指向 $CI =& get_instance();REPLACEMENT 用法
  • @Bugfixer 当您看到 404 错误时,请使用 web.archive.org 编辑该链接。该链接已完成
【解决方案2】:

get_instance() 是 CodeIgniter 核心文件中定义的函数。当您在超级对象之外的范围内时,您可以使用它来获取对 CodeIgniter 超级对象的单例引用。

我很确定它是在 base.php 或类似的东西中定义的。

【讨论】:

    【解决方案3】:

    只有扩展了 CI_Controller,Model,View 的类才能使用

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

    您的自定义类不能使用上述代码。 要在您的自定义类中使用上述功能,您必须使用

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

    例如,在您的自定义类中

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

    【讨论】:

      【解决方案4】:

      这是一个单例结构,用于了解 codeigniter 如何加载库和类

      <?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();
      

      【讨论】:

        【解决方案5】:

        $CI = get_instance();就是在helper上把$this替换成$CI,

        【讨论】:

        • 但你必须在自动加载 [library] 上定义你的助手
        【解决方案6】:

        把它放在构造函数中对我有用:

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

        【讨论】:

          猜你喜欢
          • 2012-11-20
          • 1970-01-01
          • 2011-04-08
          • 2011-11-24
          • 2017-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-29
          相关资源
          最近更新 更多