CodeIgniter 3 未定义 属性:MY_Output::$load

CodeIgniter 3 Undefined property: MY_Output::$load

我想知道我是否可以深入了解这个问题。我正在开发一个在 GoDaddy(Linux 共享主机)上运行良好的 CI3 网站,但出于某种原因,相同的代码和相同的数据库在我的本地服务器(我的 Windows 10 笔记本电脑)上产生错误。

我已经更改了所有 config/config.php 常量,以及 config/database.php 常量以反映本地设置。

我的config.phpbase_url:

$config['base_url'] = 'http://simplereo/';

我在 Windows 的主机文件中设置了虚拟服务器 simplereo,并在 Apache 的 httpd.conf 中设置了虚拟服务器,我在这方面有丰富的经验,所以本地虚拟网络服务器正在运行。

我得到的具体错误是:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: MY_Output::$load

Filename: core/My_Output.php

Line Number: 8

Backtrace:

File: C:\xampp\htdocs\simplereo\application\core\My_Output.php
Line: 8
Function: _error_handler

File: C:\xampp\htdocs\simplereo\index.php
Line: 302
Function: require_once

错误是指我的 MY_OUTPUT class 文件。 application\core\MY_OUTPUT.php 文件包含此代码:

class MY_Output extends CI_Output {

function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
    $this->load->helper('cookie');
    $this->config->load('security');
    $this->load->library('session');
    $this->load->helper('url');
}

function nocache()
{
    $this->set_header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
    $this->set_header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
    $this->set_header('Cache-Control: post-check=0, pre-check=0', FALSE);
    $this->set_header('Pragma: no-cache');
}

}

虽然 $this->load->model('user_model'); 出现在第 8 行,但我很确定该行不是具体的问题,因为我可以打乱加载顺序,但它仍然会在第 8 行出错,或者我可以完全删除第 8 行,错误将移至下一行。不确定为什么在远程服务器上一切正常,但在我的本地服务器上却不行。

* 我想这就是答案:* 我把第一个函数拿出来了:

function __construct()
{
    parent::__construct();
    $this->load->model('user_model');
    $this->load->helper('cookie');
    $this->config->load('security');
    $this->load->library('session');
    $this->load->helper('url');
}

...而且我没有收到任何错误。我什至需要 __construct() 吗?我可能是无缘无故放的。

我不确定,但一般来说,如果您覆盖或创建自己的库,$this->load 将不起作用,因为 $this 只能直接在您的控制器、模型或视图中工作。 如果您想在自己的自定义 类 中使用 CodeIgniter 的 类,您可以按如下方式进行:

$CI =& get_instance();

$CI->load->model('user_model');

PS:在 CI documentation

上阅读更多相关信息