【问题标题】:Fatal error: Call to undefined method CI_Controller::CI_Controller() in C:\xampp\htdocs\CodeIgniter\application\controllers\hello.php on line 8致命错误:在第 8 行的 C:\xampp\htdocs\CodeIgniter\application\controllers\hello.php 中调用未定义的方法 CI_Controller::CI_Controller()
【发布时间】:2013-11-06 05:40:55
【问题描述】:

致命错误:调用未定义的方法 CI_Controller::CI_Controller() 在 C:\xampp\htdocs\CodeIgniter\application\controllers\hello.php 上 第 8 行

我是框架的新手。我在做一个小应用程序时在 CodeIgniter 中遇到了上述错误。请帮助我..我的代码如下所示

<?php 
class Hello extends CI_Controller {
var $name;
var $color;

function Hello()
{
parent::CI_Controller();
$this->name= 'Andi';
$this->color= 'red';
}

function you()
{
$data['name'] = $this->name;
$data['color'] = $this->color;
$this->load->views('you_view', $data);
}
}
?>

【问题讨论】:

    标签: php codeigniter frameworks


    【解决方案1】:

    替换这个函数:

    function Hello()
    {
        parent::CI_Controller();
        $this->name     =   'Andi';
        $this->color    =   'red';
    }
    

    与:

    public function __construct() {
        parent::__construct();
        $this->name     =   'Andi';
        $this->color    =   'red';
    }
    

    注意:在最新版本的 Codeigniter 中,您需要
    使用构造函数而不是类名作为构造函数。

    【讨论】:

      【解决方案2】:

      您使用了 php 5,而在 php 5 中编写构造函数的方式是 __construct(),因为 php 4 允许开发人员将构造函数编写为您在代码中使用的类名。

      所以,请将你的构造函数写成...

      function __construct()
      {
          parent::CI_Controller();
          $this->name= 'Andi';
          $this->color= 'red';
      }
      

      我想你的问题会解决的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-20
        • 2013-11-04
        • 2013-02-18
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        • 2012-12-08
        相关资源
        最近更新 更多