CodeIgniter中的__construct()函数是什么作用?

3

我使用CodeIgniter框架,并且我是新手。在下面的代码中,__construct()函数用于加载模型。

  • 为什么需要使用__construct()?
  • 应该在什么时候使用这个函数?
  • parent::__construct()是什么意思?

代码:

function __construct() {
    parent::__construct();
    $this->load->model('example');
}

我认为在下一个版本中,父构造函数将被取消,而使用模型。 - Alex
7个回答

3
构造函数允许您在整个类中使用事物。这样,您就不必在每个方法中加载模型/语言/设置。
例如,如果您有一个要为该类加载的模型和语言,可以在构造函数中执行此操作。如果您在类中只有一个电子邮件方法并且仅在该类中使用电子邮件,则不必在构造函数中设置它,而是在该方法中设置。这样,它不会被不需要使用的所有其他方法加载。
    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允许开发人员为类声明构造函数方法。拥有构造函数方法的类会在每个新创建的对象上调用此方法,因此适用于对象在使用之前需要进行的任何初始化。


这个符号叫什么:“->”,它的用途是什么? - Gem

1

你可以查看CodeIgniter文档

这些方法是您类的构造函数。

如果您需要设置一些默认值或在实例化类时运行默认流程,则构造函数非常有用。构造函数不能返回值,但它们可以执行一些默认操作。

如果您打算在任何控制器中使用构造函数,则必须放置 __construct 方法。


0

1:为什么我需要使用__construct()? 2:我应该在什么时候使用这个函数? 3:parent::__construct()是什么?

1-答案:使用__Construct()的原因是预定义的,它在C.I中作为全局函数加载任何函数。

2-答案:最好将此函数用作全局函数。假设您想要在函数A、函数B和函数C中加载任何模型。当您加载模型时,请在Construct()中加载它,它将作为全局函数工作,并在所有函数中加载。

3-答案:对所有函数全局有效。

敬礼


0

如果您需要设置一些默认值或在实例化类时运行默认进程,则构造函数非常有用。构造函数不能返回值,但它们可以执行一些默认工作。


0

构造函数可以应用于控制器,这将帮助您一次性加载库和助手,而无需在每个要使用它们的方法中重写以重新加载它们...!以下是我们可以使用构造函数的示例

在这行代码中,我向您展示了我们可以在哪里使用它。

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

}
?>

你将在单个控制器中加载库和助手。我们加载这些文件以执行一些功能,例如获取用户数据会话,添加表单验证(如强制用户填写所有必填字段)等。

你可以阅读更多关于构造函数的内容


0
 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');
      }

-1
<?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
你能为你的代码添加一些解释吗?你认为它为什么能解决这个问题? - Nico Haase

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接