要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数.这个函数返回一个CodeIgniter super object.php
通常来讲在你的控制器函数中你能够经过 $this 调用任何可用的CodeIgniter函数:session
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');函数
$this, 只直接做用在你本身的控制器,模型和视图中.当你在自定义类中想使用CodeIgniter原始类时,你能够这样作:this
首先,定义CodeIgniter对象赋给一个变量:url
$CI =& get_instance();对象
一旦定义某个对象为一个变量,你就能够使用那个变量名 取代 $this:资源
$CI =& get_instance();get
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');it
注意: 你将注意到get_instance()这个函数经过被引用的方式被传递:io
$CI =& get_instance();
这十分重要. 经过引用的方式赋给变量将使使用原始的CodeIgniter对象,而不是建立一个拷贝
同时,请注意: 若是你使用php 4,那么请最好不要在类的构造函数中调用 get_instance() .php4在引用位于构造函数中的CI super object时存在问题,由于对象只有在类彻底实例化后才存在.