CodeIgniter Forums
Constructor error? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Constructor error? (/thread-51861.html)



Constructor error? - El Forum - 05-21-2012

[eluser]Hamed[/eluser]
I use one of website tutorial and now I get error on constructor:


Fatal error: Call to undefined method CI_Controller::Controller() in C:\wamp\www\application\controllers\main.php on line 5

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {
    public function Main() {
        parent::Controller();
    }
public function index()
{
    $query = $this->db->query('');
    $this->load->view('main/index');
}
}



Constructor error? - El Forum - 05-21-2012

[eluser]CroNiX[/eluser]
It must be outdated. We use php5's __construct() for constructors now.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main extends CI_Controller {
  //The constructor
  public function __construct() {
      parent::__construct();
  }
  public function index()
  {
    $query = $this->db->query('');
    $this->load->view('main/index');
  }
}

But the main error you are seeing is because you used "parent::Controller();" instead of "parent::CI_Controller();", but that's moot if you use the new construct above. Please see the user guide for Controllers (this is mentioned at the bottom)