tags:

views:

164

answers:

2

As far as Kohana is concerned, can you give me a short sentence or two as to WHEN and WHY I need to use a constructor within my controller? After a LOT of reading, I can't seem to wrap my tiny little brain around the constructor concept. Looking for "layman's terms" here. =)

Edit: Question pertains to Kohana v2.3.4

A: 

You see each of your controllers extends the parent controller. For the parent controller to run or import it's functionality into your controller, you need to a constructor in your controller. The parent adds/sets set's functionality behavior of your controllers.

Hope that makes sense, thanks :)

Sarfraz
+1  A: 

From The Documentation:

If you declare a constructor in your controller, for example to load some resources for the entire controller, you have to call the parent constructor.

public function __construct()
{
    parent::__construct(); // This must be included

    $this->db = Database::instance();
    $this->session = Session::instance();
}

You can see in this example, the documentation demonstrates calling the parent constructor, and then setting up some properties for the class itself that will reference the database connection, and the session.

Jonathan Sampson