我正在摆弄自动加载,并试图制作一个符合 PSR-0 标准的文件结构。但是我收到此错误:
Warning: require(GetMatchHistory\api.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Test\Test.php on line 15
Fatal error: require(): Failed opening required 'GetMatchHistory\api.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\Test\Test.php on line 15
这是我的文件结构:
我正在使用带有此自动加载器功能的测试 PHP:
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
spl_autoload_register('autoload');
$Query = new GetMatchHistory_api();
这个函数是从这里建议的 PSR-0 函数复制粘贴的
我对结构应该如何有误解吗?“api.php”中的类是GetMatchHistory_api
编辑:另一个问题
我已经使用了 MrCode 建议的答案,但是现在我遇到了一个问题,即自动加载器不会加载另一个目录中的类。
use Classes\Queries\History\GetMatchHistoryAPI;
use Classes\Queries\History\GetMatchHistoryBASE;
use Classes\Utilities\send;
当我send
从里面的函数调用类时GetMatchHistoryAPI
,我收到错误:
Warning: require(Classes\Queries\History\send.php): failed to open stream: No such file or directory in C:\xampp\htdocs\Test\Test.php on line 15
但是,从上图中可以看出,发送类不在该文件路径中。为什么会出现这个错误?