Php 我的核心类在codeigniter中找不到我的头

Php 我的核心类在codeigniter中找不到我的头,php,codeigniter,class,core,Php,Codeigniter,Class,Core,我正在尝试在codeigniter.in application/core中创建一个名为MY_head.php的文件,MY_head.php的代码是 class MY_head extends CI_Controller{ public function __construct(){ parent::__construct(); } public function load_header(){ //some code here }

我正在尝试在codeigniter.in application/core中创建一个名为MY_head.php的文件,MY_head.php的代码是

 class MY_head extends CI_Controller{

 public function __construct(){

     parent::__construct();
     }


  public function load_header(){
      //some code here


      }

 }
现在我试图在controller practice.php中扩展这个类,代码如下

   class Practice extends MY_head{
   public function __construct(){

     parent::__construct();

      }
  function index(){


      }


  }
但当我在浏览器中加载practice controller时,它会显示致命错误:在中找不到类“MY_head”。问题在哪里?提前谢谢
注意:$config['subclass_prefix']='MY_'

尝试将下面的函数放在配置文件的底部

/application/config/config.php

function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . '.php' );
    }
}
并扩展控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Practice extends MY_head
{
  public function __construct()
  {
     parent::__construct();
  }
}

函数\uuu自动加载($class)
已被弃用:

PHP7.x及以上版本的更新

spl_autoload_register(function($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'core/'. $class . '.php' );
    }
});

您可能不想在每次创建核心类时手动包含它或修改CI系统。您只需更改类名大小写即可让CI读取。例如
MY\u Head
MY\u Head\u Controller
。这些是类名格式应该是。 所以你的核心类名应该是

class MY_Head extends CI_Controller{

 public function __construct(){

     parent::__construct();
     }

     public function load_header(){
        //some code here
     }
 }
就这样说吧

class Practice extends MY_Head
{
  public function __construct()
  {
     parent::__construct();
  }
}
我试过了,效果很好

注意。
如果您的类前缀是
MY\u
,并且您扩展了
CI\u Controller
,那么文件名必须是
MY\u Controller.php
,为什么我需要包含这个类?codeigniter不会自动加载核心类吗?只有MY_Controller.php核心类是自动加载的,而其他类不是。原因是什么?@AkshayHegde有正确的答案。(使用
函数自动加载($class)…
。原因是CodeIgniter允许您重载同名的核心类。即:
MY_Controller
,而不是
MY_Head
。要自动加载特定类型的类,必须以CI的标准方式以外的方式自动加载。我将以下代码放在config.php中,但错误仍然相同。…函数\u autoload($class){if(strpos($class,'CI')!==0){@include_once(APPPATH.core/'.$class.EXT);}}未定义可为常量。请尝试使用:
APPPATH.core/'.$class..php'
class Practice extends MY_Head
{
  public function __construct()
  {
     parent::__construct();
  }
}