【发布时间】:2011-06-07 14:27:12
【问题描述】:
我一直想知道是否可以将另一个对象分配给 $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));
}
}
在被调用的类中:
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.
}
}
MY_Module:
Class My_Module {
public function __construct() {
$this =& CI_Controller::get_instance(); // Here is problem.
}
}
我想对 My_Module 类使用实例化类的声明。这样它就不会初始化相同的库,也不会花费更多的资源。
我怎样才能做到这一点?
感谢您的建议。
编辑:我试图从 CI_Controller 扩展 MY_Module。但是由于它已经实例化了一次,所以它会导致问题。
【问题讨论】:
-
不知道,要开始,因此只是:永远不要再考虑这样的事情。 ;)
-
请告诉我你没有在 PHP 4.x 上工作...否则,
$this =& CI_Controller::get_instance();和号是无用的,因为在 PHP 5.x 中所有对象总是通过引用传递(因为它们应该)
标签: php class codeigniter controller this-pointer