0

I was wondering if I could have more than one custom class in CodeIgniter 3? I currently have a custom class called AC_Controller which extends the CI_Controller.

I have put this in my config

/*
|--------------------------------------------------------------------------
| Autoload Custom Controllers
|--------------------------------------------------------------------------
|
*/
function __autoload($class) {
    if (substr($class,0,3) !== 'AC_') {
        if (file_exists($file = APPPATH . 'core/' . $class . '.php')) {
            include $file;
        }
    }
}

This allows my AC_Controller to load but when I add another controller called AC_Repeat and reference it in a controller, I get this error Fatal error: Class 'AC_Repeat' not found.

So did I do something wrong or is only one custom class allowed?

4
  • Is your AC_repeat.php class file saved in core/? According to this answer, CI would load the AC_Controller class automatically anyway if you told it the prefix was AC_ in the config, but not the AC_Repeat because it's not a core class name it knows. Commented Feb 20, 2017 at 12:45
  • Yeah they're all in core. Commented Feb 20, 2017 at 12:56
  • It surprisingly loads if I place the contents of AC_Repeat in AC_Controller Commented Feb 20, 2017 at 12:56
  • 1
    Here's an article covering 4 methods to autoload your own classes. Option 2 covers this method but it looks different. It's also from May 2015, so things may have changed. The official CI way (I guess) is to use hooks. Commented Feb 20, 2017 at 13:03

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.