php 调用未定义的方法 CI_Controller::Controller()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9314502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:32:25  来源:igfitidea点击:

Call to undefined method CI_Controller::Controller()

phpcodeigniter-2

提问by BlackFire27

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?

如何防止发生此类错误?

回答by Kokers

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

如果您使用的是 CI 2.x,那么您的类构造函数应如下所示:

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

read more in user guide

用户指南中阅读更多内容

回答by Kemal Fadillah

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

在 CodeIgniter 2 中,构造函数被命名__constructor而不是类名。所以你需要打电话parent::__construct()而不是parent::Controller()

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

这是一篇您可以阅读的文章,其中显示了 CodeIgniter 1.x 和 CodeIgniter 2.x 之间的一个主要区别

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

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