我的核心 class My_head 在 codeigniter 中找不到

My core class My_head not found in codeigniter

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

 class MY_head extends CI_Controller{

 public function __construct(){

     parent::__construct();
     }


  public function load_header(){
      //some code here


      }

 }

现在我正尝试在我的控制器中扩展这个 class practice.php 代码是

   class Practice extends MY_head{
   public function __construct(){

     parent::__construct();

      }
  function index(){


      }


  }

但是当我在浏览器中加载练习控制器时,它显示致命错误:Class '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();
  }
}

或手动添加

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

// include the base class
require_once("application/core/MY_head.php");

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

?>

function __autoload($class) 已弃用:

更新 PHP 7.x 及以上

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

您可能不想在每次制作核心 class 时手动包含它或修改 CI 系统。您只需更改您的 class 姓名大小写,以便 CI 阅读。例如 MY_HeadMY_Head_Controller。这些是 class 名称格式应该是。 所以你的核心class名字应该像

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

我试过了并且有效。

注. 如果您的 class 前缀是 MY_ 并且您扩展 CI_Controller.

,则文件名必须是 MY_Controller.php