简体   繁体   中英

codeigniter, class is not extending from libraries folder

Here is what i did.

My settings in applications/config/config.php file

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

Created file MY_Controller.php in application/core

MY_Controller.php file includes:

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

created file Frontend_Controller.php in application/libraries

Frontend_Controller.php file includes

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

In last I extended the main contrller class here with Frontend_Controller my main controller resides in 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);
    }
}

Problem: So here comes the problem, when i extend the main controller class with MY_Controller , it works perfectly fine,

But when i try to extend the main controller with Frontend_Controller class it gives me this below problem

Fatal error: Class 'Frontend_Controller' not found in C:\\xampp\\htdocs\\projects\\PrizeBondSearch\\application\\controllers\\main.php on line 3

Any Ideas How to Resolve it?

No worries, Found the Solution at Last.

Needed to load the library classname.

So Added the Below lines in the config.php file.

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

It's working perfectly fine now.

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

Some content of the accepted answer are deprecated in PHP.

Use this function instead in your config.php file

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);
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM