【问题标题】:My core class My_head not found in codeigniter在 codeigniter 中找不到我的核心类 My_head
【发布时间】:2017-05-27 23:07:54
【问题描述】:

我正在尝试在 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(){


      }


  }

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

【问题讨论】:

    标签: php codeigniter class core


    【解决方案1】:

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

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

    【讨论】:

    • 为什么我需要包含这个类? codeigniter 不会自动加载核心类吗?
    • 只有 MY_Controller.php 核心类是自动加载的,其他类没有。可能是什么原因?
    • @AkshayHegde 有正确的答案。 (使用function __autoload($class)...。原因是CodeIgniter 允许您重载核心类具有相同名称。即:MY_Controller 但不是MY_Head。要自动加载某种类型的类,您必须自动加载它们超出了 CI 的标准方式。
    • 我将以下代码放入 config.php 但错误仍然相同.....function _autoload($class) { if(strpos($class, 'CI ') !== 0) { @include_once( APPPATH . 'core/'. $class . EXT ); } }
    • 可能是未定义的常量。尝试:APPPATH . 'core/'. $class . '.php'
    【解决方案2】:

    function __autoload($class) 已弃用:

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

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

    【讨论】:

      【解决方案3】:

      您可能不想在每次创建核心类时手动包含它或修改 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_ 并且您扩展了 CI_Controller,则文件名必须是 MY_Controller.php

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-12
        • 1970-01-01
        • 2021-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多