Class Constructors for CI Controllers

Why I Copy other People’s Writings

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.

<?php
class Blog extends CI_Controller {

       public function __construct()
       {
            parent::__construct();
            // Your own constructor code
       }
}
?>

Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can’t return a value, but they can do some default work.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in CodeIgniter. Bookmark the permalink.

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s