What is the function of __construct() in CodeIgniter?
I work with CodeIgniter framework and I'm new to this. In the following code, __construct()
function is used for loading a model.
- Why do I need to use
__construct()
? - When should I use this function?
- What is
parent::__construct()
?
Code
function __construct() {
parent::__construct();
$this->load->model('example');
}
php codeigniter construct
add a comment |
I work with CodeIgniter framework and I'm new to this. In the following code, __construct()
function is used for loading a model.
- Why do I need to use
__construct()
? - When should I use this function?
- What is
parent::__construct()
?
Code
function __construct() {
parent::__construct();
$this->load->model('example');
}
php codeigniter construct
I think parent construct is getting axed in the next version for models.
– Alex
Feb 24 '18 at 19:18
add a comment |
I work with CodeIgniter framework and I'm new to this. In the following code, __construct()
function is used for loading a model.
- Why do I need to use
__construct()
? - When should I use this function?
- What is
parent::__construct()
?
Code
function __construct() {
parent::__construct();
$this->load->model('example');
}
php codeigniter construct
I work with CodeIgniter framework and I'm new to this. In the following code, __construct()
function is used for loading a model.
- Why do I need to use
__construct()
? - When should I use this function?
- What is
parent::__construct()
?
Code
function __construct() {
parent::__construct();
$this->load->model('example');
}
php codeigniter construct
php codeigniter construct
edited Feb 24 '18 at 15:43
Andrew T.
4,34452849
4,34452849
asked Jun 3 '16 at 20:04
mohsenmohsen
17113
17113
I think parent construct is getting axed in the next version for models.
– Alex
Feb 24 '18 at 19:18
add a comment |
I think parent construct is getting axed in the next version for models.
– Alex
Feb 24 '18 at 19:18
I think parent construct is getting axed in the next version for models.
– Alex
Feb 24 '18 at 19:18
I think parent construct is getting axed in the next version for models.
– Alex
Feb 24 '18 at 19:18
add a comment |
3 Answers
3
active
oldest
votes
The construct function lets you use things over the entire class.
This way you don't have to load the model/language/settings in every method.
Say you have a model and language that you want to load for that class, you can do this in the constructor. If you have for example an email method in the class and only use email in that class, you dont have to set this in the constructor but in the method. This way it doesn't get loaded unneeded for all the other methods that dont use it.
class Contact extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('contact_model', 'contact');
}
public function index(){
$data['contact'] = $this->contact->getContact();
$this->load->view('contact', $data);
}
public function send_mail(){
/* Mail configuration - ONLY USED HERE */
$config = array('protocol' => 'mail',
'wordwrap' => FALSE,
'mailtype' => 'html',
'charset' => 'utf-8',
'crlf' => "rn",
'newline' => "rn",
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$records = $this->contact->getCompany();
$this->email->from( $setmail, $records['CompanyName'] );
$this->email->to( $to );
$this->email->subject( $subject );
$this->email->message( $html );
$this->email->send();
}
}
From php: http://php.net/manual/en/language.oop5.decon.php
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
add a comment |
<?php
class Stud_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view',$data);
}
public function add_student_view() {
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student() {
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function update_student_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit',$data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data,$old_roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function delete_student() {
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
}
?>
4
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
add a comment |
function __construct(){
parent::__construct();
//predefined view,models,etc..,
}
__construct function let's you define model,view,helper and other libraries, on the top of a class.
it's these model,views are belongs to that class
so, you no need to be load these for each function you call or create
once it's create it's take care of the rest of your class.
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37622520%2fwhat-is-the-function-of-construct-in-codeigniter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The construct function lets you use things over the entire class.
This way you don't have to load the model/language/settings in every method.
Say you have a model and language that you want to load for that class, you can do this in the constructor. If you have for example an email method in the class and only use email in that class, you dont have to set this in the constructor but in the method. This way it doesn't get loaded unneeded for all the other methods that dont use it.
class Contact extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('contact_model', 'contact');
}
public function index(){
$data['contact'] = $this->contact->getContact();
$this->load->view('contact', $data);
}
public function send_mail(){
/* Mail configuration - ONLY USED HERE */
$config = array('protocol' => 'mail',
'wordwrap' => FALSE,
'mailtype' => 'html',
'charset' => 'utf-8',
'crlf' => "rn",
'newline' => "rn",
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$records = $this->contact->getCompany();
$this->email->from( $setmail, $records['CompanyName'] );
$this->email->to( $to );
$this->email->subject( $subject );
$this->email->message( $html );
$this->email->send();
}
}
From php: http://php.net/manual/en/language.oop5.decon.php
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
add a comment |
The construct function lets you use things over the entire class.
This way you don't have to load the model/language/settings in every method.
Say you have a model and language that you want to load for that class, you can do this in the constructor. If you have for example an email method in the class and only use email in that class, you dont have to set this in the constructor but in the method. This way it doesn't get loaded unneeded for all the other methods that dont use it.
class Contact extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('contact_model', 'contact');
}
public function index(){
$data['contact'] = $this->contact->getContact();
$this->load->view('contact', $data);
}
public function send_mail(){
/* Mail configuration - ONLY USED HERE */
$config = array('protocol' => 'mail',
'wordwrap' => FALSE,
'mailtype' => 'html',
'charset' => 'utf-8',
'crlf' => "rn",
'newline' => "rn",
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$records = $this->contact->getCompany();
$this->email->from( $setmail, $records['CompanyName'] );
$this->email->to( $to );
$this->email->subject( $subject );
$this->email->message( $html );
$this->email->send();
}
}
From php: http://php.net/manual/en/language.oop5.decon.php
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
add a comment |
The construct function lets you use things over the entire class.
This way you don't have to load the model/language/settings in every method.
Say you have a model and language that you want to load for that class, you can do this in the constructor. If you have for example an email method in the class and only use email in that class, you dont have to set this in the constructor but in the method. This way it doesn't get loaded unneeded for all the other methods that dont use it.
class Contact extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('contact_model', 'contact');
}
public function index(){
$data['contact'] = $this->contact->getContact();
$this->load->view('contact', $data);
}
public function send_mail(){
/* Mail configuration - ONLY USED HERE */
$config = array('protocol' => 'mail',
'wordwrap' => FALSE,
'mailtype' => 'html',
'charset' => 'utf-8',
'crlf' => "rn",
'newline' => "rn",
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$records = $this->contact->getCompany();
$this->email->from( $setmail, $records['CompanyName'] );
$this->email->to( $to );
$this->email->subject( $subject );
$this->email->message( $html );
$this->email->send();
}
}
From php: http://php.net/manual/en/language.oop5.decon.php
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
The construct function lets you use things over the entire class.
This way you don't have to load the model/language/settings in every method.
Say you have a model and language that you want to load for that class, you can do this in the constructor. If you have for example an email method in the class and only use email in that class, you dont have to set this in the constructor but in the method. This way it doesn't get loaded unneeded for all the other methods that dont use it.
class Contact extends CI_Controller {
public function __construct(){
parent::__construct();
$this->load->model('contact_model', 'contact');
}
public function index(){
$data['contact'] = $this->contact->getContact();
$this->load->view('contact', $data);
}
public function send_mail(){
/* Mail configuration - ONLY USED HERE */
$config = array('protocol' => 'mail',
'wordwrap' => FALSE,
'mailtype' => 'html',
'charset' => 'utf-8',
'crlf' => "rn",
'newline' => "rn",
'send_multipart' => FALSE
);
$this->load->library('email', $config);
$records = $this->contact->getCompany();
$this->email->from( $setmail, $records['CompanyName'] );
$this->email->to( $to );
$this->email->subject( $subject );
$this->email->message( $html );
$this->email->send();
}
}
From php: http://php.net/manual/en/language.oop5.decon.php
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.
edited Jun 4 '16 at 12:35
answered Jun 4 '16 at 12:10
Davy BaccaertDavy Baccaert
114
114
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
add a comment |
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
What is the name of this symbol : " -> " , and what is the purpose?
– Gem
Jun 19 '18 at 6:15
add a comment |
<?php
class Stud_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view',$data);
}
public function add_student_view() {
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student() {
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function update_student_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit',$data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data,$old_roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function delete_student() {
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
}
?>
4
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
add a comment |
<?php
class Stud_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view',$data);
}
public function add_student_view() {
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student() {
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function update_student_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit',$data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data,$old_roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function delete_student() {
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
}
?>
4
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
add a comment |
<?php
class Stud_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view',$data);
}
public function add_student_view() {
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student() {
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function update_student_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit',$data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data,$old_roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function delete_student() {
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
}
?>
<?php
class Stud_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view',$data);
}
public function add_student_view() {
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student() {
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function update_student_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit',$data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data,$old_roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function delete_student() {
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
}
?>
edited Nov 27 '18 at 13:22
AS Mackay
2,0034820
2,0034820
answered Nov 27 '18 at 13:08
user10711735user10711735
1
1
4
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
add a comment |
4
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
4
4
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
Can you add some explanation to your code? Why do you think that it solves the problem?
– Nico Haase
Nov 27 '18 at 14:18
add a comment |
function __construct(){
parent::__construct();
//predefined view,models,etc..,
}
__construct function let's you define model,view,helper and other libraries, on the top of a class.
it's these model,views are belongs to that class
so, you no need to be load these for each function you call or create
once it's create it's take care of the rest of your class.
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
New contributor
add a comment |
function __construct(){
parent::__construct();
//predefined view,models,etc..,
}
__construct function let's you define model,view,helper and other libraries, on the top of a class.
it's these model,views are belongs to that class
so, you no need to be load these for each function you call or create
once it's create it's take care of the rest of your class.
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
New contributor
add a comment |
function __construct(){
parent::__construct();
//predefined view,models,etc..,
}
__construct function let's you define model,view,helper and other libraries, on the top of a class.
it's these model,views are belongs to that class
so, you no need to be load these for each function you call or create
once it's create it's take care of the rest of your class.
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
New contributor
function __construct(){
parent::__construct();
//predefined view,models,etc..,
}
__construct function let's you define model,view,helper and other libraries, on the top of a class.
it's these model,views are belongs to that class
so, you no need to be load these for each function you call or create
once it's create it's take care of the rest of your class.
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
New contributor
New contributor
answered Feb 25 at 7:14
teenage vampireteenage vampire
11
11
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37622520%2fwhat-is-the-function-of-construct-in-codeigniter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I think parent construct is getting axed in the next version for models.
– Alex
Feb 24 '18 at 19:18