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 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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题