Welcome Guest, Not a member yet? Register   Sign In
Call to a member function library() on a non-object
#1

[eluser]linusan[/eluser]
Hi, I'm getting more and more frustrated while trying to fix and understand the error I'm getting (since yesterday). I even don't understand why there is a problem at all. The controller looks exactly like all my other controllers (which work perfectly) and I can't find any typo in this one as well. I really hope someone can prevent me from getting insane...

Here is the whole error:
Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Search::$load
Filename: controllers/search.php
Line Number: 11
Fatal error: Call to a member function library() on a non-object in /htdocs/findjou/application/controllers/search.php on line 11

So, on a page there is a (search)form, which links to the controller search and the function search within.
Code:
echo form_open('search/search');
...

The Controller search.php:
Code:
<?php

class Search extends CI_Controller {

/*function __construct(){
  parent::__construct();
}*/

function search(){

  $this->load->library('form_validation');

  $this->form_validation->set_rules('keyword', 'Keyword', 'trim|min_length[4]');

  if($this->form_validation->run() == FALSE) {
   redirect('talents/show');
  } else {
   $this->load->model('search_model');
   $data['result'] =$this->search_model->search();

   $data['main_content'] = 'search';
   $this->load->view('includes/login_template', $data);
  }
}

}

The database is auto loaded from the config. The error occurs as well when I try to comment the library-load or the model-load.

I really don't understand why this error only occurs with this controller/function. My other controllers and functions look exactly the same and work perfectly. Even those which manage the login functions and other things, which as well are called by a form. I can't notice any difference.

PS: While trying to debug this error I found a piece of code which was meant to solve the error (which failed), but what exactly does this do?
Code:
*function __construct(){
  parent::__construct();
}

Thank you so much!
#2

[eluser]aquary[/eluser]
Why did you comment out the constructor > >?
It's for initialize all CI_Controller stuff including CI core itself. Without it, nothing related to CI would work. I'm guessing if you comment out the $this->load->library() line, you'll get another error on the next line also.
#3

[eluser]linusan[/eluser]
When I uncomment it, the new error is:

404 Page Not Found
The page you requested was not found.

I commeted it out because I hoped it would help. None of my other controllers seemed to need it because they work perfectly. Should they have a constructor as well?

#4

[eluser]aquary[/eluser]
Hmm, they are not required, but I always include it so...

Anyway, it shouldn't be the source of that not found error. The case here is indeed weird..

What happen if you rename your function name to something else? It did catch my eyes that your function has the same name as the class. When you do that, the function will act as a constructor, like the one you comment out. And if you have a constructor, you'll have to put parent::__construct() as a first line. That would be the answer why your other controller are OK.

Either change the function name, or add parent::__construct() to the first line of the function should fix the problem.
#5

[eluser]linusan[/eluser]
Aaah ok, I renamed the function to searchDB instead and removed the constructor again it worked! Thank you so much for the explanation, it really helps me a lot understanding what happened! Otherwise I would for sure have made this mistake again and again. Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB