Welcome Guest, Not a member yet? Register   Sign In
Extending CI_Controller / Indirect modification of overloaded property
#1

[eluser]popovich[/eluser]
Hello,

I've been seeing this error for a long time now, 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.

In my application/config/routes I have this
Code:
$route['default_controller'] = "dispatcher";

The Dispatcher class in application/controllers/ is defined as this:
Code:
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:
Code:
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 )

Every model I use starts like this (named accordingly):
Code:
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:
Quote: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
Code:
if ( $mapped = $this->map->get_map() ){

the line 82 in the other model looks like this:
Code:
$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.
Where is the problem?

Thanks a lot in advance!
#2

[eluser]popovich[/eluser]
I know this might not be necessarily the problem of Codeigniter, but of PHP OOP syntax/logic – I just hope someone can help me with that here.
How do you do that in your controllers/models?
#3

[eluser]popovich[/eluser]
I should probably add that MY_Controller, which extends CI_Controller, has this __set function
Code:
public function __set ($key, $value ) {
  $this->GLO[$key] = $value;
}

If I delete these lines, I get a longer list of "indirect modification of overloaded property" warnings.
Still no clues?
#4

[eluser]popovich[/eluser]
Ok, here is a more simple question: within CI framework, how do you properly define a variable (array) which is available from any model, library, helper, etc?

PS. It is weird to be talking to myself...
#5

[eluser]Abdul Malik Ikhsan[/eluser]
try this :
Code:
$ci = & get_instance();
        $ci->variableofmee = "hei, it is me";
#6

[eluser]popovich[/eluser]
OK, this works.
In MY_Controller at the end of __constructor:
Code:
$CI =& get_instance();
  $CI->testing = array("test"=>"testing 123");
  $CI->GLO = $this->GLO;

In Dispatcher class (which extends MY_Controller):
Code:
print_r($this->testing);
  print_r($this->GLO);
  exit;

the output is as expected. However the override error persists.
O_o
#7

[eluser]Abdul Malik Ikhsan[/eluser]
not like that, for example like this :

Code:
class MY_Controller extends CI_Controller
{
    protected $ci;

    public function __construct()
    {
        $this->ci = & get_instance();
        $this->ci->testing = array('test'=>'testing 123');
    }
}
and in your disapther class :
Code:
class Dispatcher extends MY_Controller
{
    protected $ci;

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

    public function index()
    {
        print_r($this->ci->testing);
    }
}
#8

[eluser]popovich[/eluser]
The "testing" array can be read alright – as it was the case before. However it still cannot be modified from within loaded models.
#9

[eluser]Abdul Malik Ikhsan[/eluser]
it can. place $this->load->model('yourmodel') after parent::__construct()
Code:
class Dispatcher extends MY_Controller
{
    protected $ci;

    public function __construct()
    {  
        parent::__construct();
        $this->load->model('yourmodel');
    }
}
and in your model, you have to re-instance the object
Code:
class yourmodel
{
     public function getTesting()
    {
       $ci = & get_instance();
       print_r($ci->testing);
     }
}
#10

[eluser]popovich[/eluser]
Thanks for bearing with me.
But isn't this method just setting testing[key] within a newly created instance only? I guess so, cause even though I have got rid of errors, the original testing array was not modified – it was probably modified only within the model and the new values are still not available within the Dispatcher class. Undecided




Theme © iAndrew 2016 - Forum software by © MyBB