php – CodeIgniter HMVC扩展了MX_Controller,无法正确使用get_instance

前端之家收集整理的这篇文章主要介绍了php – CodeIgniter HMVC扩展了MX_Controller,无法正确使用get_instance前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在/ application / core中有一个控制器

/application/core/CMS_Controller.PHP

<?PHP if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require APPPATH."third_party/MX/Controller.PHP";

class CMS_Controller extends MX_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function show_something() {
        echo "something shown";
    }
}

我在模块中有另一个控制器(/modules/my_module/controllers/controller.PHP),它从CMS_Controller扩展而来

/modules/my_module/controllers/controller.PHP

<?PHP if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Controller extends CMS_Controller {

    public function index() {
        $this->load->view('view');
    }
}

并且,在view.PHP(/modules/my_module/views/view.PHP)中我这样做:
/modules/my_module/views/view.PHP

<?PHP if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 $ci =& get_instance();
 echo $ci->show_something();
?>

我收到这个错误

Fatal error: Call to undefined method CI::show_something() in
/home/gofrendi/public_html/No-CMS/modules/my_module/views/view.PHP on
line 3

如果我不使用MX_Controller并使用CI_Controller,它将起作用:
/application/core/CMS_Controller.PHP

<?PHP if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//require APPPATH."third_party/MX/Controller.PHP";

class CMS_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function show_something() {
        echo "something shown";
    }
}

谁知道这里有什么问题?

在application / third_party / MX / Controller.PHP
在构造函数的末尾(在第54行之后)
我补充道
/* allow CI_Controller to reference MX_Controller */
CI::$APP->controller = $this;

如果你看代码$this指的是当前的类,它是MX_Controller,CI :: $APP是指CI_controller(查看MX / Base.PHP文件)

所以现在很简单……
获取对CI_Controller的引用
我们会做(按照正常情况)

$this->CI =& get_instance();

并且要获得对MX_Controller的引用

$this->CI =& get_instance()->controller;

猜你在找的PHP相关文章