CodeIgniter中__construct()的功能是什么?

问题描述 投票:2回答:5

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

  • 为什么我需要使用__construct()
  • 我什么时候应该使用这个功能?
  • 什么是parent::__construct()

function __construct() {
    parent::__construct();
    $this->load->model('example');
}
php codeigniter construct
5个回答
1
投票

构造函数允许您在整个类中使用事物。这样您就不必在每种方法中加载模型/语言/设置。

假设您有要为该类加载的模型和语言,可以在构造函数中执行此操作。如果您在类中有一个电子邮件方法,并且只在该类中使用电子邮件,则不必在构造函数中设置此方法,而是在方法中。这样,对于不使用它的所有其他方法,它不会被加载。

    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允许开发人员为类声明构造函数方法。具有构造函数方法的类在每个新创建的对象上调用此方法,因此它适用于对象在使用之前可能需要的任何初始化。


1
投票

你可以查看CodeIgniter documentation

这些方法是你班级的构造者。

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

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


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

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 

}
?>

您将在单个Controller上加载库和帮助程序。我们加载这些文件来执行一些功能,比如获取用户数据会话,在表单中添加验证,以强制用户填写所需的所有输入域等。

You can read more about Contruct


-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); 
      } 
   } 
?>
© www.soinside.com 2019 - 2024. All rights reserved.