Codeigniter controller constructor


Codeigniter Controller constructor : Are special functions which are called when a new object is created.


Syntax : Codeigniter controller constructor

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mycontroller extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            $this->load->helper('date');
            // More code goes here...
       }
      
      public function index(){
           // if you load date helper in contructor you need not load here 
           // all the helper date functions will be available here.
          
       }
}

Note parent::__construct(); is required to override the parent class’s constructor.

If you load date helper in contructor you need not load date helper again in any function date helper will be available in all functions inside this controller.


Advertisements

Add Comment

📖 Read More