Call to undefined method CI_Controller::Controller()
Asked Answered
P

3

8

I have got this controller:

    class Start extends CI_Controller{
   var    $base;
   var    $css;

   function Start()
   {
       parent::Controller(); //error here.
       $this->base = $this->config->item('base_url'); //error here
       $this->css = $this->config->item('css');   

   }

  function hello($name)
  {
    $data['css'] = $this->css;
    $data['base'] = $this->base;
    $data['mytitle'] = 'Welcome to this site';
    $data['mytext'] = "Hello, $name, now we're getting dynamic!";
    $this->load->view('testView', $data);
   }
}

it tells me in this line:

parent::Controller(); //error here.

 Call to undefined method CI_Controller::Controller() 

If I remove that line..I get an error for the next line that says..

Call to a member function item() on a non-object

How do I prevent such errors form happening?

Phillisphilly answered 16/2, 2012 at 15:56 Comment(0)
C
26

If you're using CI 2.x then your class constructor should look like this:

   public function __construct()
   {
        parent::__construct();
        // Your own constructor code
   }

read more in user guide

Cultigen answered 16/2, 2012 at 16:2 Comment(0)
L
3

In CodeIgniter 2, the constructor is named __constructor and not the class name. So you need to call parent::__construct() instead of parent::Controller()

Here's an article that you can read that shows one major difference between CodeIgniter 1.x and CodeIgniter 2.x

http://ulyssesonline.com/2011/03/01/differences-between-codeigniter-1-7-2-and-2-0-0/

Lodhia answered 16/2, 2012 at 16:2 Comment(0)
S
0

If you're running your Codeigniter project via Xampp or a similar server add the following code to the bottom of your config.php file in the following directory; ci_project/application/config/config.php

function my_load($class) {        

    if (strpos($class, 'CI_') !== 0) {            
        if (is_readable(APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php' )) {                
            require_once (APPPATH . 'core' . DIRECTORY_SEPARATOR . $class . '.php');                
        }
    }

}

spl_autoload_register('my_load');

The above code would help to; load class in core folder. I'm certain this works in the following setup; CI-3+, Xampp, Php5.6, and or 5.6+

Also, you can then decide to create and allow other classes to reference your own Controller (which extends the original CI_Controller) by creating a file named MY_Controller.php in the following directory: ci_project/application/core/ and adding the following code in it;

<?php
    class MY_Controller extends CI_Controller {

    }
?>

That way you can always just reference or extend the other Classes to your own Controller (MY_Controller) throughout the rest of the project e.g.

class Admin extends MY_Controller {
    //your function here
}

I hope this helps.

Stableman answered 16/6, 2021 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.