doubaolan2842
2015-04-23 09:16
浏览 231
已采纳

致命错误:在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?

图片转代码服务由CSDN问答提供 功能建议

我在 Application \ core 下创建了一个模型 Admin_Model 代码点火器目录。 我把所有基本数据库操作都放在它下面。 当我尝试扩展 Application \ model 目录下的模型时,会抛出错误。

致命错误:找不到类'Admin_Model' &lt;第3行的root&gt; /application/models/new_model.php的路径

我是否应该错过任何配置?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanmeng1858 2016-12-06 10:58
    已采纳

    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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqol6556 2015-04-23 10:45

    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();
        }
    }
    
    评论
    解决 无用
    打赏 举报
  • dousu8456 2017-02-13 02:25

    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);
                }
        }
    });
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题