dsf8897
2015-01-31 11:38 阅读 31
已采纳

如何在Codeigniter中的抽象类中获取get_instance?

There are three classes:

Standard controller class:

class LoaderContent extends CI_Controller {
        public function __construct(){
        $obj = new SearchNew();
      }
}


abstract class Search
{
// Get here access to get_instance
}


class SearchNew extends Search
{
    public function __construct()
    {

        parent::__construct($type);
    }
}

How to get access to a Codeigniter instance in an abstract class?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douzi8548 douzi8548 2015-01-31 20:34

    In Codeigniter, get_instance() is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it returns the Controller class instance). First, assign the CodeIgniter object to a variable:

    $CI =& get_instance();
    

    Once you've assigned the object to a variable, you'll use that variable instead of $this:

    $CI =& get_instance(); 
    $CI->load->helper('url'); 
    $CI->load->library('session');
    $CI->config->item('base_url');
    

    etc.

    Note: You'll notice that the above get_instance() function is being passed by reference:

    $CI =& get_instance();
    
    点赞 评论 复制链接分享

相关推荐