扩展CI_Controller/间接修改过载属性


Extending CI_Controller / Indirect modification of overloaded property

CI社区似乎对此没有答案(或者可能它太明显了,看起来像是一个noob问题?),所以我在这里问。

我已经看到这种错误很长时间了(错误消息随之而来),但我一直使用这种"肮脏"的解决方法。现在我不想再编辑核心CI代码了,我知道必须有一个优雅的解决方案。

这是设置。

在我的应用程序/config/routes中,我有这个

$route['default_controller'] = "dispatcher";

应用程序/控制器/中的Dispatcher类定义如下:

class Dispatcher extends MY_Controller {
 function __construct() {
  parent::__construct();
  $this->load->helper(array('form','https'));
 }

 function index() {
  # load models
  $this->load->model('site/map');
  $this->load->model('site/langs');
  $this->load->model('site/pages');
  if ( $mapped = $this->map->get_map() ){ // this is the line 21
   $this->langs->set_user_lang( $this->GLO['show_lang_in_url'], $this->GLO['show_lang_at_home'] );
   $red = base_url().$this->GLO['user_lang_label']."/".$mapped;
   redirect($red, "refresh");
   } 
  ...

现在MY_Controller驻留在应用程序/核心中,定义如下:

class MY_Controller extends CI_Controller {
 public function __construct() {
        parent::__construct();
  $this->GLO = array();   
  #
  # set defaults:
  #
  $this->GLO['deb'] = '';
  $this->GLO['baseurl'] = base_url();
  ... (more GLO[key] = value ) 
}
 public function __set ($key, $value ) {
  $this->GLO[$key] = $value;
 }

我使用的每个模型都是这样开始的(相应地命名):

class Map extends CI_Model {
 function __construct(){
  parent::__construct(); 
 }
 function get_map(){
        .....
        return true;
        }

现在,在Debian上使用CI 2.1.2、PHP 5.3.3、Apache 2加载页面时,我收到了以下错误:

遇到PHP错误严重性:通知消息:间接修改重载属性Langs:$GLO无效文件名:site/langs.php行号:82

遇到PHP错误严重性:注意消息:未定义属性:Dispatcher::$map文件名:controllers/Dispatcher.php行编号:21

致命错误:对中的非对象调用成员函数get_map()/home/webdev/MC/_WWW/application/controllers/dispatcher.php在线21

线21是上面在地图模型中标记的线

if ( $mapped = $this->map->get_map() ){

另一个模型中的线82看起来是这样的:

$this->GLO['langs'][] = $temp;

在整个代码中,此GLO数组被引用为$this GLO[key]。它必须不时地读写。如果我删除MY_Controller中的__set函数,我会收到更多的警告。

问题出在哪里?

提前感谢!

我不确定第一个错误,但这两个:

遇到PHP错误严重性:注意消息:未定义属性:Dispatcher::$map文件名:controllers/Dispatcher.php行编号:21

致命错误:对中的非对象调用成员函数get_map()/home/webdev/MC/_WWW/application/controllers/dispatcher.php在线21

看起来你没有正确加载你的模型?

更改

if ( $mapped = $this->map->get_map() ){

$this->load->model('map');
if ( $mapped = $this->map->get_map() ){