拽拽
管理员组

CodeIgniter之$CI =& get_instance();


要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数.这个函数返回一个CodeIgniter super object.

一般来说在你的控制器函数中你可以通过 $this 调用任何可用的CodeIgniter函数:

$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
etc.
$this, 只直接作用在你自己的控制器,模型和视图中.当你在自定义类中想使用CodeIgniter原始类时,你可以这样做:

首先,定义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对象,而不是创建一个拷贝

同时,请注意: 如果你使用php 4,那么请最好不要在类的构造函数中调用 get_instance() .php4在引用位于构造函数中的CI super object时存在问题,因为对象只有在类完全实例化后才存在.
#1楼
发帖时间:2016-10-21   |   查看数:0   |   回复数:0
游客组