目录结构如下

index.php

<?php
define('BASEDIR', __DIR__);
include BASEDIR . '/Mode/Loader.php';
spl_autoload_register('\\Mode\\Loader::autoload');
App\Controller\Home\Index::test();

Mode/Loader.php

<?php
namespace Mode;
class Loader
{
    static function autoload($class)
    {
        require BASEDIR . '/' . str_replace('\\', '/', $class) . '.php';
    }
}

App/Controller/Home/Index.php

<?php
namespace App\Controller\Home;
class Index
{
    static function test()
    {
        echo __METHOD__;
    }
}

执行结果

C:\phpStudy2018\PHPTutorial\php\php-7.2.1-nts\php.exe C:\wwwroot\study\index.php
App\Controller\Home\Index::test
Process finished with exit code 0

发表评论

电子邮件地址不会被公开。 必填项已用*标注