Why is this constructor failing to load in Code Ig

2019-03-18 21:02发布

问题:

<?php
class Blog extends CI_Controller {
    function Blog() {
        parent::CI_Controller();
    }
}

I'm trying to create a constructor in Code Igniter for my class 'Blog' and the above code is giving me a fatal error:

Fatal error: Call to undefined method CI_Controller::CI_Controller() in C:\xampp\htdocs\mysites\blog\application\controllers\blog.php on line 5

How do I fix this?

(I'm going through a online video tutorial on the official code igniter website but I think the tutorial is about 2 years out of date as some of the things are not working when I follow them exactly as shown in the video, this being one of them - the link to the video is here - I encounter this problem towards the end of the tutorial about 8 minutes in)

回答1:

It should be this...

<?php
class Blog extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

}

The tutorial you are probably going through is based on 1.7.2 which had a core of php4 which did not use the php5 __construct() method of building Class constructors. Codeigniter 2.0.0 has a php5 core and uses it.