Val*_*our 2 php controller codeigniter class this-pointer
我一直想知道是否有可能将另一个对象分配给$ this?
在CodeIgniter中,我从主控制器调用另一个控制器.
应用/控制器/ module.php
Class Module extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function _remap($class, $args = array()) {
// doing some checks that is there a valid file, class and method
// assigning the $method.
// including MY_Module class. I'am extending every module from this class.
// then:
$EG = new $class();
call_user_func_array(array(&$EG, $method), array_slice($this->uri->rsegments, 3));
}
}
Run Code Online (Sandbox Code Playgroud)
在被叫班级:
Class Users extends MY_Module
{
public function __construct() {
parent::__construct();
}
public function index() {
// Want to use this class and method like it is a codeigniter controller.
}
}
Run Code Online (Sandbox Code Playgroud)
MY_Module:
Class My_Module {
public function __construct() {
$this =& CI_Controller::get_instance(); // Here is problem.
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用实例化的类'声明到My_Module类.因此它不会初始化相同的库,也不会花费更多的资源.
我怎么能做到这一点?
谢谢你的建议.
编辑:我试图从CI_Controller扩展MY_Module.但由于它已经实例化了一次,因此造成了问题.
您不能"注入"对当前$this对象的引用.即使这是可能的,我强烈建议反对.
只需将引用分配给另一个var并从中使用即可.
public function __construct() {
$yourSingleton = CI_Controller::get_instance();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1089 次 |
| 最近记录: |