Link to home
Start Free TrialLog in
Avatar of Zoe Zoe
Zoe Zoe

asked on

constructor - CodeIgniter

Can anyone tell me why is the constructor in news.php using parent::__construct ? I only know it is because this is to use the method in parent class which is  within CI_Controller.
If so, why is the constructor in news_model.php not using parent::__construct?
News.php
News-model.php
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
A constructor is just like any other method.  It only differs in that it is called once automatically when an object is instantiated from the class.  

In the general sense, calling parent::__construct() is just calling a function (method) to get some piece of work done.  Different programming objectives may need different approaches.  If the child class needs the parent's constructor to run before it can do its work, calling the parent constructor makes sense.  If this is not needed (for example, the child simply adds some static functionality) then there is no reason to call the "upstream" constructors or other methods.
If so, why is the constructor in news_model.php not using parent::__construct?
 As for my knowledge it is implemented to limit such idea from model constructor where controller does.   I agree with  @Marco Gasi comments