解释$ CI =& get_instance();

时间:2011-01-19 20:38:54

标签: php codeigniter

查看codeigniter的源代码,

在其辅助函数中我一直看到代码 $CI =& get_instance(); 任何人都可以向我解释这段代码是如何工作的?

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

6 个答案:

答案 0 :(得分:70)

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

要深入了解,请查看source code

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

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

因此,如果你的应用只是PHP5, 应该而不是做CI_Base::get_instance();,而是相同的......

答案 1 :(得分:18)

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

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

答案 2 :(得分:3)

这是一个单例结构,用于理解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();

答案 3 :(得分:2)

只有扩展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 :(得分:1)

$ CI = get_instance();是在辅助程序上将$ this替换为$ CI,

答案 5 :(得分:0)

将其放入构造函数中对我有用:

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