PSR-0自动加载测试问题

时间:2014-03-12 10:59:30

标签: php namespaces autoload psr-0

我正在摆弄自动加载,并试图制作一个符合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

这是我的文件结构:

File structure

我正在使用Test 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功能here

复制粘贴

我是否误解了结构应如何? “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

但是,从上图中可以看出,send类不在该文件路径中。为什么会出现此错误?

1 个答案:

答案 0 :(得分:2)

基于该结构,您需要将类重命名为Classes_Queries_GetMatchHistory_api并将实例化的代码更改为:

$Query = new Classes_Queries_GetMatchHistory_api();

原因是您的Test.php位于根目录,而api类位于Classes/Queries/GetMatchHistory目录中。


使用命名空间而不是下划线方法的示例:

api.php:

namespace Classes\Queries\GetMatchHistory;

class api
{

}

test.php的:

spl_autoload_register('autoload');
$Query = new Classes\Queries\GetMatchHistory\api();

或使用use

use Classes\Queries\GetMatchHistory\api;

spl_autoload_register('autoload');
$Query = new api();

解决你的意见:

  

我将拥有相同的类名,但是在不同的文件夹下(所以GetMatchHistory - api.php和GetMatchDetails - api.php)。我想在调用类时会使它太模糊了

命名空间旨在解决这个问题。命名空间允许您具有相同名称的类(但在不同的名称空间中)并避免任何冲突。

例如,您在apiGetMatchHistory下都有GetMatchDetails个课程。

文件:Classes / Queries / GetMatchHistory / api.php

namespace Classes\Queries\GetMatchHistory;

class api
{
    public function __construct(){
        echo 'this is the GetMatchHistory api';
    }
}

文件:Classes / Queries / GetMatchDetails / api.php

namespace Classes\Queries\GetMatchDetails;

class api
{
    public function __construct(){
        echo 'this is the GetMatchDetails api, I am separate to the other!';
    }
}

文件:Test.php(用法示例)

spl_autoload_register('autoload');

$historyApi = new Classes\Queries\GetMatchHistory\api();
$detailsApi = new Classes\Queries\GetMatchDetails\api();

如果您愿意,可以提供别名,而不是键入整个完全限定的命名空间:

use Classes\Queries\GetMatchHistory\api as HistoryApi;
use Classes\Queries\GetMatchDetails\api as DetailsApi;

$historyApi = new HistoryApi();
$detailsApi = new DetailsApi();

正如您所看到的,命名空间可以使多个不同的类具有相同的名称,而不会产生冲突或使其模糊不清。