My core class My_head not found in codeigniter

[亡魂溺海] 提交于 2019-12-24 04:14:12

问题


i am trying to create core class in codeigniter.in application/core in create a file with the name of MY_head.php and the code of MY_head.php is

 class MY_head extends CI_Controller{

 public function __construct(){

     parent::__construct();
     }


  public function load_header(){
      //some code here


      }

 }

Now i am trying to extend this class in my controller practice.php the code is

   class Practice extends MY_head{
   public function __construct(){

     parent::__construct();

      }
  function index(){


      }


  }

but when i load practice controller in browser it says Fatal error: Class 'MY_head' not found in. where the problem is? Thanks in advance Note : $config['subclass_prefix'] = 'MY_';


回答1:


Try putting below function at the bottom of config file

/application/config/config.php

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

and Extend Controller

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

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

OR include manually

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

?>



回答2:


function __autoload($class) is deprecated:

Update for PHP 7.x and up

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


来源:https://stackoverflow.com/questions/41620013/my-core-class-my-head-not-found-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!