Welcome Guest, Not a member yet? Register   Sign In
Beginner question
#1

[eluser]Uplift[/eluser]
I am in the process of learning CI but one thing i'm just not sure what it's for

Code:
class Welcome extends CI_Controller {

    function index() {
    
        parent::__construct();
        
                $data['something'] = "something";
                $this->load->view('includes/header', $data);


    }
}

parent::__construct(); .. what it is this for, what is it actually doing?

I've removed it from my code and it still works fine. I've checked a few guides, most say it's needed but nothing more.
#2

[eluser]WanWizard[/eluser]
The Welcome class extends the CI_Controller class.

parent::__construct() calls the constructor of the parent class. What is does depends on the code in that constructor.
#3

[eluser]cideveloper[/eluser]
personally I use constructors to do things that will be needed before running any functions in the class. specifically I use it to load libraries, helpers and models that I know will be used in all or most of my functions. I also like to throw this

Code:
//$this->output->enable_profiler(TRUE);

in the constructor. That way If I am having problems I just uncomment it and all the functions will show the profiler.




Theme © iAndrew 2016 - Forum software by © MyBB