CodeIgniter Forums
Construct errors - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Construct errors (/showthread.php?tid=28656)



Construct errors - El Forum - 03-17-2010

[eluser]toadi[/eluser]
It seems that I can't find the error. I just try to setup a controller that checks if I'm logged in. If logged in I see the home view else I go to create user page.

But I have problems with the contructs. I'm a bit baffled about CI's implementation of PHP5 and PHP4. Don't really get it how to do it in CI.


MY_Controller.php
Code:
Class MY_Controller extends Controller
{
    
    function __construct()
    {
        parent::Controller;
        
        
    }
        
    
}

class Auth_Controller extends MY_Controller
{
    
    function __construct()    
    {
        parent::MY_Controller;
        
        $this->load->library('Ion_auth');

        if (!$this->ion_auth->loggedin())
        {
            header('Location: /auth/create_user');
        }
        
    }
}

Home.php
Code:
if (! defined('BASEPATH')) exit('No direct script access');

class Home extends Auth_Controller {


    function __construct()
    {
          parent::Auth_Controller();
    
    }
    
    function index() {
        
    $this->load->view('home');
    
    }

}

The error:

Code:
Fatal error: Call to undefined method Auth_Controller::Auth_Controller() in /Applications/MAMP/htdocs/simpleBI/system/application/controllers/home.php on line 10



Construct errors - El Forum - 03-17-2010

[eluser]cahva[/eluser]
If you are going to use PHP5 style __construct, use it also in the parent::__construct() to be consistent. Oh yeah and parent::Controller; != parent::Controller(); so add those () to the end.

My_Controller.php
Code:
Class MY_Controller extends Controller
{
    
    function __construct()
    {
        parent::__construct();
    }
        
    
}

class Auth_Controller extends MY_Controller
{
    
    function __construct()    
    {
        parent::__construct();
        
        $this->load->library('Ion_auth');

        if (!$this->ion_auth->loggedin())
        {
            header('Location: /auth/create_user');
        }
        
    }
}

home.php
Code:
if (! defined('BASEPATH')) exit('No direct script access');

class Home extends Auth_Controller {


    function __construct()
    {
          parent::__construct();
    
    }
    
    function index() {
        
        $this->load->view('home');
    
    }

}


If that still doesnt work, make sure you have copied MY_Controller.php to the libraries folder, not controllers(someone might get confused with this).


Construct errors - El Forum - 03-18-2010

[eluser]toadi[/eluser]
I should have gone to bed and sleep and looked again to the code. I had it correct in the beginning but forgot the () after construct. Because of being very tired and not seeing this I started second guessing myself and looking somewhere else.

After a good nights rest I can move on again.