我是codeigniter的新手。
我想知道$CI =& get_instance();的用法。
这是用于错误记录还是全局配置变量。
提前谢谢。
发布于 2011-01-07 17:29:04
摘自CodeIgniter手册:
$CI =& get_instance();
将对象赋给一个变量后,您将使用该变量而不是$this:
$CI =&get_instance();$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url'); etc.注意:您会注意到上面的get_instance()函数是通过引用传递的:
$CI =& get_instance();这一点非常重要。通过引用赋值,您可以使用原始CodeIgniter对象,而不是创建其副本。
另外,请注意:如果你运行的是PHP4,通常最好避免在类构造函数中调用get_instance()。PHP 4很难在应用程序构造函数中引用CI超级对象,因为在类完全实例化之前,对象是不存在的。
链接:http://codeigniter.com/user_guide/general/creating_libraries.html
发布于 2017-04-28 16:47:30
只有扩展CI_Controller、模型、视图的类才能使用
$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.https://stackoverflow.com/questions/4624061
复制相似问题