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 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理