douna1892 2012-09-13 15:16
浏览 58

扩展CI_Controller /间接修改重载属性[重复]

This question already has an answer here:

the CI community seems to have no answer to this (or maybe it is so obvious, that it looks like a noob question?), so I ask here.

I've been seeing this kind of error for a long time now (error messages follow), however I have always used this "dirty" workaround. Now I do not want to edit the core CI code anymore, and I know there must be a graceful solution to this.

Here is the setup.

In my application/config/routes I have this

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

The Dispatcher class in application/controllers/ is defined as this:

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");
   } 

  ...

now MY_Controller resides in application/core and is defined as follows:

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;
 }

Every model I use starts like this (named accordingly):

class Map extends CI_Model {

 function __construct(){
  parent::__construct(); 
 }

 function get_map(){
        .....
        return true;
        }

Now with CI 2.1.2, PHP 5.3.3, Apache 2 on Debian when trying to load a page, I am getting this error:

A PHP Error was encountered Severity: Notice Message: Indirect modification of overloaded property Langs::$GLO has no effect Filename: site/langs.php Line Number: 82

A PHP Error was encountered Severity: Notice Message: Undefined property: Dispatcher::$map Filename: controllers/dispatcher.php Line Number: 21

Fatal error: Call to a member function get_map() on a non-object in /home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21

The line 21 is the one marked above in the Map model

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

the line 82 in the other model looks like this:

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

Throughout the code, this GLO array is referenced as $this-GLO[key]. It must be read and written from time to time. If I delete the __set function in MY_Controller, I get more of those warnings.

Where is the problem?

Thanks a lot in advance!

</div>
  • 写回答

1条回答 默认 最新

  • dpoh61610 2012-09-13 15:47
    关注

    I'm not sure about the first error - but these two:

    A PHP Error was encountered Severity: Notice Message: Undefined property: Dispatcher::$map Filename: controllers/dispatcher.php Line Number: 21

    Fatal error: Call to a member function get_map() on a non-object in /home/webdev/MC/_WWW/application/controllers/dispatcher.php on line 21

    ...look like you havent loaded your model correctly?

    change

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

    to

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

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行