php CodeIgniter中__construct()的作用是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37622520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the function of __construct() in CodeIgniter?
提问by mohsen
I work with CodeIgniter framework and I'm new to this. In the following code, __construct()
function is used for loading a model.
我使用 CodeIgniter 框架,我是新手。在以下代码中,__construct()
函数用于加载模型。
- Why do I need to use
__construct()
? - When should I use this function?
- What is
parent::__construct()
?
- 为什么我需要使用
__construct()
? - 我应该什么时候使用这个功能?
- 什么是
parent::__construct()
?
Code
代码
function __construct() {
parent::__construct();
$this->load->model('example');
}
回答by Davy Baccaert
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' => "\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();
}
}
From php: http://php.net/manual/en/language.oop5.decon.php
来自 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.
PHP 5 允许开发人员为类声明构造函数方法。具有构造函数方法的类在每个新创建的对象上调用此方法,因此它适用于对象在使用之前可能需要的任何初始化。
回答by Fizik26
You can check the CodeIgniter documentation
您可以查看CodeIgniter 文档
Theses methods are the constructors of your class.
这些方法是您的类的构造函数。
Constructors are useful if you need to set some default values, or run a default process when your class is instantiated. Constructors can't return a value, but they can do some default work.
如果您需要设置一些默认值,或者在实例化类时运行默认进程,则构造函数很有用。构造函数不能返回值,但它们可以做一些默认的工作。
If you intend to use a constructor in any of your Controllers, you MUST place the __construct
method.
如果您打算在任何控制器中使用构造函数,则必须放置该__construct
方法。
回答by Aamir Khan
1: Why do I need to use __construct()? 2: When should I use this function? 3: What is parent::__construct()?
1:为什么我需要使用__construct()?2:我应该什么时候使用这个功能?3:什么是 parent::__construct()?
1-Ans : The reason of using __Construct() is predefined and it is used in C.I as global to load any function
1-Ans : 使用 __Construct() 的原因是预定义的,它在 CI 中用作全局加载任何函数
2-Ans : Best use of this function is as global .. just suppose if you want to load any model in function A, and function B and function C. So When you load the model then load it in Construct() and it works as Global and it loads in all functions.
2-Ans :这个函数的最佳用途是作为全局 .. 假设你想在函数 A、函数 B 和函数 C 中加载任何模型。所以当你加载模型时,然后在 Construct() 中加载它并且它工作作为全局,它加载在所有功能中。
3-Ans : Global to all functions
3-Ans : 全局所有功能
Regards
问候
回答by teenage vampire
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.
__construct 函数让您在类的顶部定义模型、视图、帮助程序和其他库。正是这些模型,视图属于该类,因此,您无需为您调用或创建的每个函数加载它们,一旦创建它就会处理您的类的其余部分。
public function __construct(){
parent::__construct();
$this->load->model('your_model_name');
$this->load->view('your_view_name');
}
回答by Nimes
construct can be applied on the controller this will help you to load library and helper at once you don't need to rewrite on each method that you want to use it in order to reload them...! example where we can use the construct
构造可以应用于控制器,这将帮助您立即加载库和助手,您无需重写要使用它的每个方法以重新加载它们......!我们可以使用构造的示例
at this line of code I show you where we can use it.
在这行代码中,我向您展示了我们可以在哪里使用它。
<?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
}
?>
You will load library and helper at single Controller. We load these file to perform some of functionality like to get user data session, to add validation in form like to force user to fill all the input fiel required etc.
您将在单个控制器上加载库和助手。我们加载这些文件以执行一些功能,例如获取用户数据会话,在表单中添加验证,例如强制用户填写所需的所有输入字段等。
回答by user10711735
<?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);
}
}
?>