怎么在CI框架中使用附属类-创新互联
                                            怎么在CI框架中使用附属类?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

任何在你的控制器方法中初始化的类都可以简单的通过get_instance() 函数来访问 CodeIgniter 资源。这个函数返回一个 CodeIgniter 对象。
通常来说,调用 CodeIgniter 的方法需要使用$this
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');但是$this 只能在你的控制器、模型或视图中使用,如果你想在 你自己的类中使用 CodeIgniter 类,你可以像下面这样做:
首先,将 CodeIgniter 对象赋值给一个变量:
$CI =& get_instance();
一旦你把 CodeIgniter 对象赋值给一个变量之后,你就可以使用这个变量 来 代替$this
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');如果你在类中使用``get_instance()`` 函数,好的方法是将它赋值给 一个属性 ,这样你就不用在每个方法里都调用get_instance() 了。
例如:
class Example {
  protected $CI;
  // We'll use a constructor, as you can't directly call a function
  // from a property definition.
  public function __construct()
  {
    // Assign the CodeIgniter super-object
    $this->CI =& get_instance();
  }
  public function foo()
  {
    $this->CI->load->helper('url');
    redirect();
  }
  public function bar()
  {
    $this->CI->config->item('base_url');
  }
}在上面的例子中,foo() 和bar() 方法在初始化 Example 类之后都可以正常工作,而不需要在每个方法里都调用get_instance() 函数。
关于怎么在CI框架中使用附属类问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
本文标题:怎么在CI框架中使用附属类-创新互联
新闻来源:http://www.scyingshan.cn/article/csdhod.html

 建站
建站
 咨询
咨询 售后
售后
 建站咨询
建站咨询 
 