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
PHPBootstrap

Avatar of undefined
Last Comment
Loganathan Natarajan
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo