为什么在 Codeigniter 中出错

Why ERROR in Codeigniter

我有错误,请帮助

A PHP Error was encountered

Severity: Notice

Message: Undefined property: User::$user_model

Filename: controllers/user.php

Line Number: 15

Backtrace:

File: C:\wamp\www\apcodeigniter\application\controllers\user.php Line: 15 Function: _error_handler

File: C:\wamp\www\apcodeigniter\index.php Line: 292 Function: require_once

第 15 行是:public function get()

public function get()
{
    $this->user_model->get();
}

您还没有加载模型。

尝试将 User 控制器中的 get 方法更改为 -

public function get()
{
    $this->load->model('user_model');
    $this->user_model->get();
}

我通常在控制器中执行的操作将取决于该模型,并且每种方法都需要该模型的某些方法。

/*
 * Load the model in the controller's constructor
 */
class User extends CI_Controller
{
    function __construct()
    {
        parent::_construct(); //This is important if you're adding a constructor in the Controller, so that it can properly initialize the scope of CI_Controller in itselves
        $this->load->model(array('user_model'));
    }

    public function get() //public here really isn't necessary, but kept it for understanding
    {
        $this->user_model->get();
    }
}

希望对您有所帮助。