调用未定义的方法 CI_Controller::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);
}
}
它在这一行告诉我:
parent::Controller(); //这里出错。
Call to undefined method CI_Controller::Controller()
如果我删除该行..我会收到下一行的错误,其中显示..
Call to a member function item() on a non-object
如何防止发生此类错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 CI 2.x,那么您的类构造函数应如下所示:
在用户指南
If you're using CI 2.x then your class constructor should look like this:
read more in user guide
在 CodeIgniter 2 中,构造函数名为
__constructor
而不是类名。因此,您需要调用parent::__construct()
而不是parent::Controller()
您可以阅读这篇文章,其中显示了 CodeIgniter 1.x 之间的一个主要区别和 CodeIgniter 2.x
http://ulyssesonline。 com/2011/03/01/codeigniter-1-7-2-and-2-0-0/之间的差异/
In CodeIgniter 2, the constructor is named
__constructor
and not the class name. So you need to callparent::__construct()
instead ofparent::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/
如果您通过 Xampp 或类似服务器运行 Codeigniter 项目,请将以下代码添加到以下目录中的 config.php 文件底部; ci_project/application/config/config.php
上面的代码会有所帮助;在 core 文件夹中加载类。
我确信这在以下设置中有效; CI-3+、Xampp、Php5.6 和/或 5.6+
另外,您可以决定创建并允许其他类引用您自己的控制器(扩展了原始 CI_Controller),方法是创建一个名为 MY_Controller.php 的文件,如下所示目录:ci_project/application/core/ 并在其中添加以下代码;
这样,您始终可以在项目的其余部分中引用或扩展其他类到您自己的控制器(MY_Controller),例如
我希望这会有所帮助。
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
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;
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.
I hope this helps.