Ajax and Ajax Frameworks

I have been a developer for nearly 20 years and a web developer for about 10 years. Over the years, I have considered creating a blog to share the things I have learned (and am learning) about web design and development, but I never seemed to have the time. When I began learning about web development using Ajax and Ajax Frameworks, I decided to take the time to create this blog.

My intention for this blog is to focus primarily on Ajax Frameworks, but since web development requires knowledge of many technologies, I will occasionally write about things such as CSS, Javascript, PHP, MySQL, Flash, etc.

Lately, I have been using JQuery as my primary Ajax tool. Although I have used other Ajax Frameworks in the past (Dojo Toolkit, Yahoo! User Interface Library, Scriptaculous/Prototype), JQuery has thus far been the easiest to learn. In my first few posts I will discuss some of the JQuery Plugins that I have created. Some of these include: a plugin for Google Maps (jquery.imGoogleMaps), Form validation and submission  (jquery.imValidateForm), Page Populater (jquery.imPagePopulate), and a plugin to create lists (jquery.imList).

While I am still learning about some of the other Ajax Frameworks, I hope that what I have learned will be helpful to others. Just remember, “When you can pull the pebbles from my hand…”

CodeIgniter: Call to a member function on a non-object

Monday, June 15th, 2009

I 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.

Related posts

7 Responses to “CodeIgniter: Call to a member function on a non-object”

  • anata_arie Says:


    Hi, I’ve try your solution to use $CI = & get_instance();
    but it didn’t work too. Have you another solution?
    Thanks
  • admin Says:


    I need more information. Can u send me code snippets and let me know exactly where you receive the error?
  • John P. Says:


    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 =-.

    My ComLuv Profile

  • admin Says:


    I copied the code directly from a project that I worked on.
  • Andy Says:


    Hi,

    I had exactly the same problem and this fixed it for me. Many thanks,

    Andy

  • yaro Says:


    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!:)

  • Dhrubo Says:


    I’ve meet this problem this morning and through your solution I’ve helped much. Thanks.

CommentLuv Enabled