【问题标题】:Extending CI_Controller / Indirect modification of overloaded property [duplicate]扩展 CI_Controller / 间接修改重载属性 [重复]
【发布时间】:2012-09-06 17:38:12
【问题描述】:

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

我已经看到这种错误很长时间了(错误消息如下),但是我一直使用this "dirty" workaround。现在我不想再编辑核心 CI 代码了,我知道必须有一个优雅的解决方案。

这是设置。

在我的应用程序/配置/路由中我有这个

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

application/controllers/中的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 位于 application/core 中,定义如下:

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 行是 Map 模型中上面标记的那一行

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

另一个模型中的第 82 行如下所示:

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

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

问题出在哪里?

提前非常感谢!

【问题讨论】:

    标签: codeigniter continuous-integration php


    【解决方案1】:

    我不确定第一个错误 - 但以下两个:

    遇到 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() ){
    

    【讨论】:

    • application/models/ 下有子文件夹,其中一个是“site”,类文件所在的位置。在 CI 文档中,这种语法实际上应该是正确的。 codeigniter.com/user_guide/general/models.html#loading
    • 尝试将“map”移动到“models”的根目录,看看会发生什么?你确定你的文件都是小写的吗?
    • 嗯,它适用于以前版本的 CI 和几个实时站点。正如我所说,如果我修改核心 CI 代码中的一行,一切都会完美无缺。这就是我想最终停止做的事情,因此问题......
    • 修改是否解决了'map'的两个错误?并且地图真的修改后还能用吗?
    • 是的,我引用的修改 - codeigniter.com/forums/viewthread/178540/#851803 - 工作正常并且不会引发任何此类错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2012-03-02
    • 2020-03-24
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    相关资源
    最近更新 更多