在codeigniter中未找到我的核心类My_head

ioekq8ef  于 2023-03-16  发布在  其他
关注(0)|答案(3)|浏览(131)

我尝试在codeigniter中创建core类,在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
  }
}

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

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

   function index(){
   }
}

但当我在浏览器中加载练习控制器时,它显示
致命错误:在中未找到类“MY_head”。
问题出在哪里?
注意:$config ['子类前缀']= 'MY_';

u0njafvf

u0njafvf1#

function __autoload($class)已弃用:
PHP 7.x及更高版本的更新

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

ulmd4ohb2#

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

/应用程序/配置文件/配置文件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();
   }
}

?>
chy5wohz

chy5wohz3#

你可能不想手动包含它或者在每次创建核心类的时候修改CI系统。你只需要改变类名的大小写,CI就可以读取它。例如MY_Head或者MY_Head_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_并且您扩展了CI_Controller,则文件名必须是MY_Controller.php

相关问题