编码器,类不是从库文件夹扩展的


codeigniter, class is not extending from libraries folder

我是这么做的

我在applications/config/config.php文件中的设置

$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
$config['url_suffix'] = '';
$config['subclass_prefix'] = 'MY_';

application/core中创建文件MY_Controller.php

MY_Controller.php文件包括:

class MY_Controller extends CI_Controller
{
    public function __construct(){
        parent::__construct();
    }
} 

application/libraries中创建文件Frontend_Controller.php

Frontend_Controller.php文件包含

class Frontend_Controller extends MY_Controller
{
    public function __construct(){
        parent::__construct();
    }
}

在最后我扩展了主控制器类这里与Frontend_Controller我的主控制器驻留在application/controllers/main.php

class Main extends MY_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->model('PrizeBondSearch_Model');
    }
    public function index()
    {
        $PrizeBonds = $this->PrizeBondSearch_Model->ShowAllPBS();
        $this->load->view('home', $PrizeBonds);
    }
}

问题:所以问题来了,当我用MY_Controller扩展主控制器类时,它工作得很好,

但是当我尝试用Frontend_Controller类扩展主控制器时,它给了我下面的问题

致命错误:类'Frontend_Controller'没有找到C: ' xampp '根' ' PrizeBondSearch ' '控制器' main.php应用项目第3行

有什么办法解决吗?

别担心,终于找到解决办法了。

需要加载库classname.

所以在config.php文件中添加了以下行

function __autoload($classname){
    if(strpos($classname, 'CI_')!==0){
        $file = APPPATH.'libraries/'.$classname.'.php';
        if(file_exists($file)&& is_file($file)){
            @include_once($file);
        }
    }
}

现在一切正常

在MY_Controller.php的开头包含Frontend_Controller.php或者使用spl_autoloader来防止这个错误

PHP不支持接受答案中的某些内容。

在config.php文件

中使用这个函数
spl_autoload_register(function ($classname) {
    if (strpos($classname, 'CI_') !== 0) 
    {
        $file = APPPATH . 'libraries/' . $classname . '.php';
        if (file_exists($file) && is_file($file)) 
        {
            @include_once($file);
        }
    }
});