升级CodeIgniter 1.7到2.1后,Undefined $ load属性错误 [英] Undefined $load property error after upgrading CodeIgniter 1.7 to 2.1

查看:152
本文介绍了升级CodeIgniter 1.7到2.1后,Undefined $ load属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在将CodeIgniter从v1.7升级到v2.1后会出现此错误?

Why I get this error after upgrading CodeIgniter from v1.7 to v2.1?

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site::$load

Filename: libraries/Website.php

Line Number: 25

Fatal error: Call to a member function library() on a non-object in C:\xampp\htdocs\travel\application\libraries\Website.php on line 25

库应用程序/库/网站

class Website extends CI_Controller {

    public static $current_city;

    public function __construct() {

        $this->load->library('language'); // line 25
        $this->language->loadLanguage();
        $this->load_main_lang_file();
        $this->load_visitor_geographical_data();
        $this->load->library('bread_crumb');
   }
}


推荐答案

忘记调用 __ construct 方法 CI_Controller 类:

public function __construct()
{
    // Call CI_Controller construct method first.
    parent::__construct();

    $this->load->library('language'); // line 25
    $this->language->loadLanguage();
    $this->load_main_lang_file();
    $this->load_visitor_geographical_data();
    $this->load->library('bread_crumb');
}

注意:如果您要创建Controller ,它应该放在 application / controllers / 中,而不是在 application / libraries /

Note: If you're creating a Controller, it should be placed in application/controllers/, not in application/libraries/.

如果child(inheritor)类有一个构造函数,父构造函数不会被调用,因为你将覆盖父构造函数和子函数,除非你使用 parent :: __ construct(); 。这是在 多态性 的概念://en.wikipedia.org/wiki/Object-oriented_programmingrel =nofollow>面向对象的编程

If the child (inheritor) class has a constructor, the parent constructor won't be called, because you'll override parent's constructor with the child one, unless you explicitly call parent's constructor using parent::__construct();. That's the concept of Polymorphism in object-oriented programming

如果你不调用 parent :: __ construct(); 当应用程序控制器正在初始化时,您将失去 Loader Core 类和 $ this-> load 将永远不会工作。

If you don't call parent::__construct(); when the application controller is initializing, you'll lose Loader and Core class and $this->load would never works.

使用 parent :: __ construct(); 只需要在你的控制器中声明 __ construct()一个。

Using parent::__construct(); is needed only if you want to declare __construct() method in your Controller which it will override the parent's one.

这也适用于模型,但在模型中使用 parent :: __ construct(); 记录调试消息模型类已初始化,因此,如果您需要知道何时初始化模型(在日志中),请继续使用,忽略它。

That's true for models as well, but using parent::__construct(); in your model just logs a debug message Model Class Initialized, So if you need to know when Model is initialized (in logs), keep using that, If not, ignore it.

这篇关于升级CodeIgniter 1.7到2.1后,Undefined $ load属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆