自定义包的 class 不会使用 composer autoloader 自动加载

Custom package's class won't auto-load with composer autoloader

我正在尝试在 Packagist.org 上发布一个新包,但我在使用自动加载器系统时遇到了一些问题。

当我安装我的包时 (https://packagist.org/packages/gabyfle/gsteam-auth) 一切正常,但是当我加载它时 PHP :

require __DIR__ . "/vendor/autoload.php";

$testing = new \Class\SteamAuth('test', 'test');

我收到一个错误,告诉我找不到 Class 命名空间。

Fatal error: Uncaught Error: Class 'Class\SteamAuth' not found in <path>\gSteam-test\testing.php:4 Stack trace: #0 {main} thrown in <path>\gSteam-test\testing.php on line 4

我已经尝试了所有不同的自动加载约定(PSR0 PSR4),我还尝试通过 classmap 参数将我的 class 包含在自动加载器中,但我的 class 没有加载。

你知道如何让我的 class 通过作曲家的自动加载器加载吗?

谢谢

我建议您使用 PSR-4 自动加载。请记住,您必须在名称空间声明的末尾放置反斜杠:

"autoload": {
    "psr-4": {
        "Gabyfle\": "src/"
    }
},

引自作曲家文档:

Note that as opposed to the older PSR-0 style, the prefix (Foo\) is not present in the file path.

还有:

Namespace prefixes must end in \ to avoid conflicts between similar prefixes. For example Foo would match classes in the FooBar namespace so the trailing backslashes solve the problem: Foo\ and FooBar\ are distinct.

因此,如果您从 psr-0 切换到 psr-4,请记住这一点

感谢@mdexp,我发现我的 class 出了什么问题。 我在一个文件中定义了两个 classes,这与 PSR-4 标准不匹配。

我刚刚从文件中删除了一个 class,现在一切正常。

谢谢!