How to use spl_autoload_register for multiple diectories in PHP?

后端 未结 2 1571
难免孤独
难免孤独 2021-01-30 11:58

I\'m actually trying to create a MVC framework for my own, however I\'m having troubles with the Autoload. It\'s not a problem actually, but I\'d like to ask the gurus, how are

2条回答
  •  半阙折子戏
    2021-01-30 12:26

    The code below will help. But I'll advice you check on Namespaces.

    spl_autoload_register ( function ($class) {
    
    $sources = array("Controllers/$class.php", "Lib/$class.php ",  "Models/$class.php " );
    
        foreach ($sources as $source) {
            if (file_exists($source)) {
                require_once $source;
            } 
        } 
    });
    

提交回复
热议问题