致命错误:在codeigniter中找不到类

I have created a model Admin_Model under Application\core directory of code igniter. I put all basic database operations under it. When I try to extend my models who are under Application\model directory, it throws error.

Fatal error: Class 'Admin_Model' not found in <path to root>/application/models/new_model.php on line 3

Should I miss any configuration?

Thanks all for your efforts. I find a solution.

By default CodeIgniter have a setting in its config file.

$config['subclass_prefix'] = 'MY_';

I just replace 'MY_' with 'Admin_' everything works fine.

$config['subclass_prefix'] = 'Admin_';

More appropriate solution is

  1. Put class file in libraries folder
  2. Add following code to config.php

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

That's All

Extending Core Class

If all you need to do is add some functionality to an existing library - perhaps add a function or two - then it's overkill to replace the entire library with your version. In this case it's better to simply extend the class. Extending a class is nearly identical to replacing a class with a couple exceptions:

The class declaration must extend the parent class. Your new class name and filename must be prefixed with MY_ (this item is configurable. See below.). For example, to extend the native Model class you'll create a file named application/core/MY_Model.php, and declare your class with:

class MY_Model extends CI_Model {

}

Note: If you need to use a constructor in your class make sure you extend the parent constructor:

class MY_Model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
}

This may also be helpful, if you still have problems with __autoload to use class from libraries. it's work for me, when I use class library tobe parent class at controller.

PHP 5 >= 5.1.2, PHP 7

this is mycode at application/config.php

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