Search code examples
codeignitererror-handlingmodelshelper

Indirect modification of overloaded property in codeigniter model when editing variable set in helper


I have setup a login helper within my codeigniter setup.

I get the codeigniter instance, call a model, and set some variables.

$CI =& get_instance();

I then use $CI->load->vars($data); to make the variables available in all my models and views.

One of the variables is as follows $data->errors=array();

The idea being that if there is an error in my model I can set an error e.g if($query->num_rows()!='1'){$this->error[]="This message does not exist !";}

Then in my controllers I can check if this variable is populated and if it is display the error.

This is however throwing up the error

Message: Indirect modification of overloaded property Mail_model::$error has no effect

This error is going over my head. I've done some research into the error but cant find any explanation in lehmans terms with an applicable solution.

Could anyone help me out?


Solution

  • You have loaded the $CI variable as a reference to itself - with =&

    You have then loaded the variables through the reference - because you used $CI->load->vars()

    There is no need to load the $data into the $CI reference.

    If you want to pass messages between models and controllers (such as error messages) - then trying to use global variables is probably not the best way.

    The best option for CodeIgniter is Flashdata - this is the exact scenario why flashdata exists. You can read all about it in the CI userguide.