CodeIgniter Forums
when to use parent::__construct(); in the class? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: when to use parent::__construct(); in the class? (/showthread.php?tid=59315)



when to use parent::__construct(); in the class? - El Forum - 09-20-2013

[eluser]Unknown[/eluser]
Dear all
I`ve started learning CI by the tutorial in the User Guide.
I found the News_model did not add the parent::__construct()
Code:
News_model extends CI_Model {
public function __construct()
...//
But the News controller starts with parent::__construct()
Code:
class News extends CI_Controller {

public function __construct()
{
  parent::__construct();
Could anyone kindly tell me the differences?
And whats the criteria of adding the parent::__construct() when creating the class?
Many thanks


when to use parent::__construct(); in the class? - El Forum - 09-20-2013

[eluser]CroNiX[/eluser]
You only need to add parent::__construct(), if you are doing something in your __construct().

just having
Code:
public function __construct()
{
  parent::__construct();
}

with nothing else doesn't do anything.

See: http://ellislab.com/codeigniter/user-guide/general/controllers.html#constructors


when to use parent::__construct(); in the class? - El Forum - 09-22-2013

[eluser]Bart v B[/eluser]
In other words, if you are planning to do things only in that object, then you can reference that in your constructor.


when to use parent::__construct(); in the class? - El Forum - 09-22-2013

[eluser]jairoh_[/eluser]
to inherit directly the attributes of the parent class. Smile