我使用CodeIgniter框架,并且我是新手。在下面的代码中,__construct()
函数用于加载模型。
- 为什么需要使用
__construct()
? - 应该在什么时候使用这个函数?
parent::__construct()
是什么意思?
代码:
function __construct() {
parent::__construct();
$this->load->model('example');
}
我使用CodeIgniter框架,并且我是新手。在下面的代码中,__construct()
函数用于加载模型。
__construct()
?parent::__construct()
是什么意思?代码:
function __construct() {
parent::__construct();
$this->load->model('example');
}
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' => "\r\n",
'newline' => "\r\n",
'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();
}
}
来自php:http://php.net/manual/en/language.oop5.decon.php
PHP 5允许开发人员为类声明构造函数方法。拥有构造函数方法的类会在每个新创建的对象上调用此方法,因此适用于对象在使用之前需要进行的任何初始化。
你可以查看CodeIgniter文档。
这些方法是您类的构造函数。
如果您需要设置一些默认值或在实例化类时运行默认流程,则构造函数非常有用。构造函数不能返回值,但它们可以执行一些默认操作。
如果您打算在任何控制器中使用构造函数,则必须放置 __construct
方法。
1:为什么我需要使用__construct()? 2:我应该在什么时候使用这个函数? 3:parent::__construct()是什么?
1-答案:使用__Construct()的原因是预定义的,它在C.I中作为全局函数加载任何函数。
2-答案:最好将此函数用作全局函数。假设您想要在函数A、函数B和函数C中加载任何模型。当您加载模型时,请在Construct()中加载它,它将作为全局函数工作,并在所有函数中加载。
3-答案:对所有函数全局有效。
敬礼
如果您需要设置一些默认值或在实例化类时运行默认进程,则构造函数非常有用。构造函数不能返回值,但它们可以执行一些默认工作。
构造函数可以应用于控制器,这将帮助您一次性加载库和助手,而无需在每个要使用它们的方法中重写以重新加载它们...!以下是我们可以使用构造函数的示例
在这行代码中,我向您展示了我们可以在哪里使用它。
<?php
Class example extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library(array('session','form_validation'));
}
public function useFile(){
//here you don't need to reload your library and helper again just type your code
}
?>
你将在单个控制器中加载库和助手。我们加载这些文件以执行一些功能,例如获取用户数据会话,添加表单验证(如强制用户填写所有必填字段)等。
function __construct(){
parent::__construct();
//predefined view,models,etc..,
}
__construct函数允许您在类的顶部定义模型、视图、帮助程序和其他库。 这些模型和视图属于该类, 因此,您无需为每个调用或创建的函数加载它们, 一旦创建,它将负责处理您的类的其余部分。
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
<?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);
}
}
?>