CodeIgniter: Call to a member function on a non-object
Monday, June 15th, 2009I recently ran into a problem using CodeIgniter where I kept receiving the error: “Call to a member function on a non-object”. This problem occurred when I was trying to use a method from one model from within another model.
I have a model named mGalleryImages. Within this model, I have a method named addGalleryImages. Within this method, I load another model and tried to call a method from the loaded model:
function addGalleryImages(){
...
$this->load->model('mUtilImages');
...
$this->mUtilImages->setImageFile($_FILES['uploadFile']);
}
I receive the error when the setImageFile method is called. The problem is that when the model sees “$this”, it is looking for a method within the mGalleryImages model. The solution was to use a CodeIgniter function named get_instance().
function addGalleryImages(){
...
$CI =& get_instance();
$this->load->model('mUtilImages');
...
$CI->mUtilImages->setImageFile($_FILES['uploadFile']);
}
Easy enough, but I would never have thought that this was the problem. I played with this code for hours, trying to figure out why it didn’t work similarly to a regular PHP class. But now I know.



October 24th, 2009 at 11:34 am
Hi, I’ve try your solution to use $CI = & get_instance();
but it didn’t work too. Have you another solution?
Thanks
October 24th, 2009 at 9:43 pm
I need more information. Can u send me code snippets and let me know exactly where you receive the error?
November 14th, 2009 at 9:31 am
I think there is a problem with your code…
function addGalleryImages(){
…
$CI =& get_instance();
$this->load->model(’mUtilImages’);
…
$CI->mUtilImages->setImageFile($_FILES['uploadFile']);
}
Shouldn’t it be:
$CI->load->model(’mUtilImages’);
?
John P.´s last blog ..Mouseterpiece Theatre =-.
November 15th, 2009 at 1:06 am
I copied the code directly from a project that I worked on.
December 15th, 2009 at 5:14 am
Hi,
I had exactly the same problem and this fixed it for me. Many thanks,
Andy
January 6th, 2010 at 4:54 am
Hi,
I searched a solution for this problem all evening yeaterday and nothing
. Today i again search and…. it’s work!!! Big big thank’s!:)
February 3rd, 2010 at 11:45 pm
I’ve meet this problem this morning and through your solution I’ve helped much. Thanks.