在codeigniter

时间:2017-01-12 18:03:34

标签: 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


      }

 }

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

   class Practice extends MY_head{
   public function __construct(){

     parent::__construct();

      }
  function index(){


      }


  }

但是当我在浏览器中加载练习控制器时,它会显示致命错误:Class' MY_head'没找到。问题出在哪里?提前致谢 注意:$ config [' subclass_prefix'] =' MY _';

3 个答案:

答案 0 :(得分:3)

尝试将以下功能放在配置文件的底部

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

?>

答案 1 :(得分:1)

function __autoload($class)已过时:

针对PHP 7.x及更高版本的更新

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

答案 2 :(得分:0)

您可能不想在每次进行核心课程时都手动添加或修改CI系统。您只需要更改您的类名大小写即可让CI读取它。例如MY_HeadMY_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_Controller.php并且扩展了MY_,则文件名必须为CI_Controller