简体   繁体   中英

How to include multiple folders and files using spl_autoload();

Im building a project that uses lots of different classes and the folder structure is going to be quite large. Im not really at a stage where I can integrate composer, (although if I am please recommend a way that I can).

How can I optimise the following function? As you can see its a little messy.

spl_autoload_register('knpv_autoloader');

function knpv_autoloader($classname){

//Admin Includes
if (strpos($classname, 'KnpvAdmin') !== false) {        
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-admin/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-admin/'.$classname.'.php';
    }
}

//Manager Includes
if (strpos($classname, 'KnpvManager') !== false) {      
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-manager/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-manager/'.$classname.'.php';
    }
}

//Supplier Includes
if (strpos($classname, 'KnpvSupplier') !== false) {     
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-supplier/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-supplier/'.$classname.'.php';
    }
}

//List queries Includes
if (strpos($classname, 'KnpvList') !== false) {     
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-list-queries/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-list-queries/'.$classname.'.php';
    }
}

//Script Includes
if (strpos($classname, 'KnpvScripts') !== false) {      
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-scripts-styles/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-scripts-styles/'.$classname.'.php';
    }
}

//Search an filter Includes
if (strpos($classname, 'KnpvSearch') !== false) {       
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-search/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-search/'.$classname.'.php';
    }
}

//Form includes
if (strpos($classname, 'KnpvForm') !== false) {     
    if (file_exists(plugin_dir_path( __FILE__ ).'/knp-form/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/knp-form/'.$classname.'.php';
    }
}

//Everything else
if (strpos($classname, 'Knpv') !== false) {     
    if (file_exists(plugin_dir_path( __FILE__ ).'/'.$classname.'.php')) {
        include_once plugin_dir_path( __FILE__ ).'/'.$classname.'.php';
    }
}


}

You can probably use composer. You need a composer.json where you configure the root paths to various namespaces. Running composer dump-autoload will then generate the classloader and you only need to include <root>/vendor/autoload.php (or somesuch) once in the main entry files of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM