Search code examples
phpclassspl-autoload-register

multiple spl_autoload_register issue


I'm working on the development of a custom framework. And I have encountered an issue when I tried to dynamise the calling of my classes.

This is a visual of my files :

enter image description here

So I decided to create a different function for each folder (libs, controllers et modeles):

function autoloadLibs($class) {
    //require the general classes
    require 'libs/' . $class . '.php';
}

function autoloadModels($class) {
    //require the models classes
    require 'models/' . $class . '.php';
}

function autoloadControllers($class) {
    //require the controllers classes
    require 'controllers/' . $class . '.php';
}

spl_autoload_register ('autoloadLibs');
spl_autoload_register ('autoloadControllers');  
spl_autoload_register ('autoloadModels');

Nevertheless I have this message : Warning: require(libs/admin.php): failed to open stream, of cours it's not the good folder. But I don't know how to fix that. Is there a good way to optimise my classes calls ?


Solution

  • After few tests, I found this solution for my case :

    set_include_path(implode(PATH_SEPARATOR, array(get_include_path(), './libs', './controllers', './models')));
    spl_autoload_register();