Extending CI_Controller / Indirect modification of

2019-08-09 02:18发布

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!

1条回答
Rolldiameter
2楼-- · 2019-08-09 03:05

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() ){
查看更多
登录 后发表回答