17

I'm very new to codeigniter , I wanted to know what is the meaning of a constructor in a controller . I saw the following code in a codeigniter tutorial -

class upload extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper(form);
    }

    // rest of the class...

My question is when is the constructor invoked - is it called every time the controller serves a request (e.g the controller class is instantiated for each request it receives?)

1
  • 1
    You can test it yourself: just var_dump(time()) in your constructor and refresh the page to see what will happen. The time string dumped out will change on page refresh, so it means the class is instantiated for each request. Commented Aug 26, 2015 at 11:35

4 Answers 4

16

Well, that's a more general PHP question. Anyway, yes, the magic method __construct() is called (automatically) upon each instantiation of the class, as you can see in the manual: http://www.php.net/manual/en/language.oop5.decon.php

Usually, in CI is not necessary to call a constructor, unless you actually want one. In the example you posted, the code loads the helper on every instantiation of the class - which is the same as loading the helper in every method, just saves a lot of typing and ensures it's not forgotten. You can alternatively put the library/helper/model you want to have alywas loaded in the respective autoload array in config/autoload.php (check "autoloading" in CI's manual)

Once you define a constructor in your child Controller you're compelled to call the parent constructor (of the mail CI_Controller class), because there is where the main CI object is created and all the classes are loaded, and you need those in your child controller too; if fail to do so your child class will construct separately and won't inherit.

I hope I made myself clear, english is not my mothertongue :)

Sign up to request clarification or add additional context in comments.

Comments

4

the constructor is magic Literally its called a magic method. what makes the constructor cool is that it will do things for you BEFORE any of the methods. So if you have an admin class, and someone should be logged in in order to access it - you can check for login in the constructor and bounce them out if they are not authorized.

in the constructor you can load the models, libraries, helpers, etc that your class needs, and they will be available for any method in the class.

you can load in variables that are used by methods. this is really useful for models.

2 Comments

Does the constructor gets called only once when the class is initialized or does it gets called every time the methods inside the controller is called?
@Prabhu only once, unless a new request arrives
0

Don't use _construct() function in latest apache & codeigniter

Use helperlin in index() function

1 Comment

Please why is this? @main-usman
0

That's a general question. Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.

$this->load->model('Model_name');

now when you write this line in your constructor you don't need to load this model again and again in your methods of that class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.