Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this constructor failing to load in Code Igniter?

<?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)

like image 492
Simon Suh Avatar asked Mar 06 '11 02:03

Simon Suh


People also ask

How to load-> Model in CodeIgniter?

If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload. php file and adding the model to the autoload array.

What is constructor in CodeIgniter?

Constructor is a function that is automatically called when instantiated. this function helps us to intialize the things that we are going to need frequently in our code like when we have to load the models of helpers like form e.t.c.


1 Answers

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.

like image 62
jondavidjohn Avatar answered Oct 08 '22 23:10

jondavidjohn