php 解释 $CI =& get_instance();

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4740430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:12:33  来源:igfitidea点击:

explain $CI =& get_instance();

phpcodeigniter

提问by Hailwood

Looking through codeigniter's source code,

查看 codeigniter 的源代码,

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

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

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

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

采纳答案by ircmaxell

It's basically a Singleton Design Patternthat 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-hackto 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...

编辑:其实,现在我明白了。为了 PHP4 的兼容性,他们不得不做一个双全局变量黑客来让它正确返回引用。否则,这些参考资料会被搞砸。而且由于 PHP4 不支持静态方法(好吧,无论如何都正确),使用该函数是更好的方法。因此,由于遗留原因,它仍然存在......

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

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

回答by Wade

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.

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

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

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

回答by Khant Wai Kyaw

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

只有扩展 CI_Controller,Model,View 的类可以使用

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

回答by souparno majumder

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

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

回答by Tofan

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

$CI = get_instance(); 是在 helper 上将 $this 替换为 $CI,