php - PSR- 0 auto 加载测试问题

标签 php namespaces autoload psr-0

我正在摆弄自动加载,并尝试创建一个符合 PSR-0 标准的文件结构。但是我收到此错误:

警告:require(GetMatchHistory\api.php):无法打开流:第 15 行 C:\xampp\htdocs\Test\Test.php 中没有此类文件或目录

fatal error :require():无法在 C:\xampp\htdocs\Test\Test 中打开所需的“GetMatchHistory\api.php”(include_path='.;C:\xampp\php\PEAR') .php 第 15 行

这是我的文件结构:

File structure

我正在使用带有此自动加载器功能的测试 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;

当我从 GetMatchHistoryAPI 内的函数调用 send 类时,我收到错误:

警告:require(Classes\Queries\History\send.php):无法打开流:第 15 行 C:\xampp\htdocs\Test\Test.php 中没有此类文件或目录

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

最佳答案

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

$Query = new Classes_Queries_GetMatchHistory_api();

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


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

api.php:

namespace Classes\Queries\GetMatchHistory;

class api
{

}

测试.php:

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

或者使用use:

use Classes\Queries\GetMatchHistory\api;

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

针对您的评论:

I was going to have the same class names, but under a different folder (so GetMatchHistory - api.php and GetMatchDetails - api.php). I guess this would make it too ambiguous when calling the classes however

命名空间就是为了解决这个问题而设计的。命名空间允许您拥有具有相同名称的类(但在不同的命名空间中)并避免任何冲突。

例如,您在 GetMatchHistoryGetMatchDetails 下都有一个 api 类。

文件:类/查询/GetMatchHistory/api.php

namespace Classes\Queries\GetMatchHistory;

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

文件:类/查询/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();

正如您所看到的,命名空间可以让多个不同的类具有相同的名称,而不会发生冲突或导致歧义。

关于php - PSR- 0 auto 加载测试问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22349373/

相关文章:

php - 从数据库读回时恢复换行符

PHP:非常简单的编码/解码字符串

vb.net - 使用 VB.NET 在两个程序集中定义相同命名空间的解决方法

php - 位于 ./foo/bar/utility/baz.php 的 Foo\Bar\Baz 类不符合 psr-4 自动加载标准。跳绳

php - 在php、mysql和ajax jquery中开发聊天模块

php - PHP 中带有许多结果的 SQL

php - 使用 jquery 将多个复选框的值添加到数据库中的同一行

wpf - System.Windows.Input 对 C++/CLI 不可用?

vector - 不允许 C++ 命名空间类型名称

php - php 类和接口(interface)自动加载的区别