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

发布于 2025-01-06 20:13:20 字数 859 浏览 1 评论 0原文

我有这个控制器:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北渚 2025-01-13 20:13:20

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

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

用户指南

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

望她远 2025-01-13 20:13:20

在 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 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/

思念绕指尖 2025-01-13 20:13:20

如果您通过 Xampp 或类似服务器运行 Codeigniter 项目,请将以下代码添加到以下目录中的 config.php 文件底部; 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');

上面的代码会有所帮助;在 core 文件夹中加载类。
我确信这在以下设置中有效; CI-3+、Xampp、Php5.6 和/或 5.6+

另外,您可以决定创建并允许其他类引用您自己的控制器(扩展了原始 CI_Controller),方法是创建一个名为 MY_Controller.php 的文件,如下所示目录:ci_project/application/core/ 并在其中添加以下代码;

<?php
    class MY_Controller extends CI_Controller {

    }
?>

这样,您始终可以在项目的其余部分中引用或扩展其他类到您自己的控制器(MY_Controller),例如

class Admin extends MY_Controller {
    //your function here
}

我希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文