PHP为另一个类分配$this

PHP为另一个类分配$this,php,class,codeigniter,controller,this-pointer,Php,Class,Codeigniter,Controller,This Pointer,我一直在想是否可以为$this分配另一个对象 在CodeIgniter中,我正在从主控制器调用另一个控制器 application/controllers/module.php Class Module extends CI_Controller { public function __construct() { parent::__construct(); } public function _remap($class, $args = array()

我一直在想是否可以为$this分配另一个对象

在CodeIgniter中,我正在从主控制器调用另一个控制器

application/controllers/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));
    }
}
在被调用的类中:

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.
    }
}
我的单元:

Class My_Module {
    public function __construct() {
        $this =& CI_Controller::get_instance(); // Here is problem.
    }
}
我想对我的_模块类使用实例化类的declaretion。这样它就不会初始化相同的库,也不会花费更多的资源

我怎样才能做到这一点

谢谢你的建议


编辑:我试图从CI_控制器扩展我的_模块。但由于它已经实例化过一次,因此会引起问题。

这在PHP中是不可能的(应该是这样的)

你应该把你的问题改成这样:

我有两个类,它们都初始化相同的库(因此会占用更多资源)。 我怎样才能避免这种情况

[此处示例]

您不能“注入”对当前
$this
对象的引用。即使这是可能的,我也强烈反对

只需将引用分配给另一个var并从中使用

public function __construct() {
    $yourSingleton = CI_Controller::get_instance(); 

}

不知道,我们要开始了,所以只是答:再也不要想这样的事情了请告诉我你没有在PHP4.x上工作。。。否则,在
$this=&CI_Controller::get_instance()
ampersand是无用的,因为在PHP5.x中,所有对象都是通过引用传递的(正如它们应该传递的那样),但这不允许我使用类似于
$this->
的语句。我总是要使用像
$this->CI->
这样的代码。这将导致我的视图文件出现问题。