1

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?

Bikram Pahi
  • 1,107
  • 1
  • 12
  • 33

4 Answers4

3

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

Bikram Pahi
  • 1,107
  • 1
  • 12
  • 33
3

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);
            }
    }
});
Iwan Firmawan
  • 125
  • 1
  • 3
  • 12
  • I am having trouble with my facebook sdk integration in CI4, you guys can look at my question and please suggest something you you can help me. https://stackoverflow.com/questions/64401885/adding-facebook-library-in-codeigniter-4 – Cypher or KJ Oct 19 '20 at 09:44
2

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();
    }
}
Baxter
  • 21
  • 5
  • Your answer is good and ok if I name my model `MY_Model` but other than that its not accepting showing error. Even if I declare my model `MY_Admin_Model` also show error. – Bikram Pahi Apr 23 '15 at 11:03
  • Core models extend only existing models, which are placed in folder **../system/core/** - such Lang, Input, Model... Admin_Model is not a core model, therefore it is also error. – Baxter Apr 23 '15 at 11:23
1

I setup my environment on a Windows machine. Then after move to production using Ubuntu, I had the class not found error.

I discovered my files extension were PHP (capital letter). Linux is case sensitive, so the file name need to be lowercase php.

James Risner
  • 5,451
  • 11
  • 25
  • 47