Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined method CI_Controller::Controller()

I have got this controller:

    class Start extends CI_Controller{
   var    $base;
   var    $css;

   function Start()
   {
       parent::Controller(); //error here.
       $this->base = $this->config->item('base_url'); //error here
       $this->css = $this->config->item('css');   

   }

  function hello($name)
  {
    $data['css'] = $this->css;
    $data['base'] = $this->base;
    $data['mytitle'] = 'Welcome to this site';
    $data['mytext'] = "Hello, $name, now we're getting dynamic!";
    $this->load->view('testView', $data);
   }
}

it tells me in this line:

parent::Controller(); //error here.

 Call to undefined method CI_Controller::Controller() 

If I remove that line..I get an error for the next line that says..

Call to a member function item() on a non-object

How do I prevent such errors form happening?

like image 926
BlackFire27 Avatar asked Feb 16 '12 15:02

BlackFire27


2 Answers

If you're using CI 2.x then your class constructor should look like this:

   public function __construct()
   {
        parent::__construct();
        // Your own constructor code
   }

read more in user guide

like image 141
Kokers Avatar answered Nov 14 '22 15:11

Kokers


In CodeIgniter 2, the constructor is named __constructor and not the class name. So you need to call parent::__construct() instead of parent::Controller()

Here's an article that you can read that shows one major difference between CodeIgniter 1.x and CodeIgniter 2.x

http://ulyssesonline.com/2011/03/01/differences-between-codeigniter-1-7-2-and-2-0-0/

like image 33
Kemal Fadillah Avatar answered Nov 14 '22 15:11

Kemal Fadillah