douxi0098 2015-08-06 07:16
浏览 73
已采纳

构造函数 - CodeIgniter

Can anyone tell me why is the constructor in controller code using parent::__construct ? I only know it is because this is to use the method in parent class which is within CI_Controller. If so, why is the constructor in model code not using parent::__construct?

class News extends CI_Controller
{

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



class News_model extends CI_Model
{
public function __construct()
{
    $this->load->database();
}
  • 写回答

2条回答 默认 最新

  • dongtu0088 2015-08-06 07:19
    关注
    • The reason this line is necessary is because your local constructor will be overriding the one in the parent controller class so we need to manually call it.
    • 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.

    Read Codeigniter Constructors

    And Possible Duplicate of PHP Codeigniter - parent::__construct

    Example

    public function  __construct()
    {
        parent::__construct();
        $this->load->helper('date');
        $this->load->library('session');
        $this->load->model('My_model');
        $this->load->library('cart');
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计