zoukankan      html  css  js  c++  java
  • php $CI =& get_instance();

    初学php 看有人这样写,$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时存在问题,因为对象只有在类完全实例化后才存在.

  • 相关阅读:
    (2/24) 快速上手一个webpack的demo
    (1/24) 认识webpack
    module.exports 、exports、export、export default的区别
    Git同时提交到多个远程仓库
    @codeforces
    @loj
    @bzoj
    @loj
    @bzoj
    @bzoj
  • 原文地址:https://www.cnblogs.com/web-ed2/p/4324171.html
Copyright © 2011-2022 走看看