Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Controller Constructor

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?)

like image 695
Joel Blum Avatar asked Nov 25 '12 21:11

Joel Blum


People also ask

What is __ construct in CodeIgniter?

The construct function lets you use things over the entire class. This way you don't have to load the model/language/settings in every method. Say you have a model and language that you want to load for that class, you can do this in the constructor.

How can a constructor be called in CodeIgniter?

Class Constructors If you intend to use a constructor in any of your Controllers, you MUST place the following line of code in it: parent::__construct(); The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.

What is parent __construct ();?

We can do this by using the special function call parent::__construct(). The "parent" part means "get the parent of this object, and use it", and the __construct() part means "call the construct function", of course. So the whole line means "get the parent of this object then call its constructor".

Where is CI_Controller in CodeIgniter?

The pages class is extending the CI_Controller class. This means that the new pages class can access the methods and variables defined in the CI_Controller class ( system/core/Controller. php ). The controller is what will become the center of every request to your web application.


2 Answers

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 :)

like image 96
Damien Pirsy Avatar answered Oct 23 '22 09:10

Damien Pirsy


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.

like image 32
cartalot Avatar answered Oct 23 '22 10:10

cartalot