php自動加載帶命名空間類的函數

  1. 代碼:
    <?php
    /*
    @desc:自動加載類的函數
    @param dir 須要註冊自動加載的文件夾
    */
    function autoloader($dir){
    spl_autoload_register(function($name) use ($dir){
        $name = str_replace('\\',DIRECTORY_SEPARATOR,$name);
        require $dir.DIRECTORY_SEPARATOR.$name.'.php';
    });
    }
  2. 測試:
    autoloader('a');
    use a\config;
    $config = new config();
    $config->say();
  3. 輸出:
    hello
相關文章
相關標籤/搜索