2

I am trying to create core class in codeigniter. In application/core I 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 the practice controller in the browser it says

Fatal error: Class 'MY_head' not found in.

Where is the problem?

Note : $config['subclass_prefix'] = 'MY_';

jps
  • 20,041
  • 15
  • 75
  • 79
Alicia Dsney
  • 69
  • 2
  • 5

3 Answers3

5

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' );
    }
});
Bart Mommens
  • 145
  • 2
  • 13
4

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

?>
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • Why do i need to include this class ? does codeigniter doesn't load the core classes automatically ? – Alicia Dsney Jan 12 '17 at 18:30
  • only MY_Controller.php core class is auto loading but not the other classes. what can be the reason ? – Alicia Dsney Jan 12 '17 at 18:39
  • 1
    @AkshayHegde has the correct answer. (use the `function __autoload($class)...`. The reason is CodeIgniter allows you to overload core classes **with the same name**. IE: `MY_Controller` but not `MY_Head`. To autoload classes of a certain type you must autoload them outside of CI's standard way. – Friderich Weber Jan 12 '17 at 18:49
  • i put the following code in config.php but the error still same.....function __autoload($class) { if(strpos($class, 'CI_') !== 0) { @include_once( APPPATH . 'core/'. $class . EXT ); } } – Alicia Dsney Jan 12 '17 at 19:01
  • 1
    Could be constant is not defined. Try with: `APPPATH . 'core/'. $class . '.php'`. – Tpojka Jan 13 '17 at 03:19
  • @Tpojka yes there is chance – Akshay Hegde Jan 13 '17 at 05:25
  • Edit yor answer and replace EXE with '.php' so i can accept it as correct one – Alicia Dsney Jan 13 '17 at 17:45
  • @AliciaDsney : actually if you define constant `EXE` in index.php, it will work, but as you said, I edited – Akshay Hegde Jan 13 '17 at 18:04
  • how to define in index.php ? – Alicia Dsney Jan 13 '17 at 19:38
  • @AliciaDsney : codeigniter main folder there will be `index.php` there you can define constant `define('EXE', '.php');` or else you can define in `application/config/constants.php` ` – Akshay Hegde Jan 13 '17 at 19:43
  • @Akhsay another question for you. https://stackoverflow.com/questions/41642379/codeigniter-controller-is-not-working – Alicia Dsney Jan 13 '17 at 19:48
0

You maybe don't want to include it manually or modify CI system each time you make a core class. You only need to change your class name case in order for CI to read it. For example MY_Head or MY_Head_Controller. Those are the class name format should be. So your core class name should be like

class MY_Head extends CI_Controller{

 public function __construct(){

     parent::__construct();
     }

     public function load_header(){
        //some code here
     }
 }

and call it like

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

I tried that and works.

Note. The file name must be MY_Controller.php if your class prefix is MY_ and you extend CI_Controller.

fsevenm
  • 791
  • 8
  • 19