php自动加载带命名空间类的函数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 aconfig;
    $config = new config();
    $config->say();
  3. 输出:
    hello

以上是关于php自动加载带命名空间类的函数的主要内容,如果未能解决你的问题,请参考以下文章