gpt4 book ai didi

php - composer dump-autoload -o 跳过我所有的类(class)

转载 作者:行者123 更新时间:2023-12-04 17:01:10 25 4
gpt4 key购买 nike

我刚刚升级到 Composer 2.0,它显然对它解析类的方式做了一些修改......composer dump-autoload -o或别名 composer du -o基本上在我的控制台中转储了很多行,文本如下:

Class MyProject\SubCategory\CoolClass located in C:/udvikling/MyProjectRoot/src/MyProjectClasses\MyProject\SubCategory\CoolClass.php does not comply with psr-4 autoloading standard. Skipping


阅读后 Composer psr-4 autoload issue我确保所有有助于我的命名空间和类路径的名称都大写。即 use myproject/helperclass现在是 use MyProject/HelperClass我的 C:/udvikling/MyProjectRoot/src/composer.json 包括以下内容
    "autoload": {
"psr-4": {
"MyProject\\": "MyProjectClasses/"
}
}

我通过在类名中添加一个前导反斜杠来实现:
<?php

namespace \MyProject\Core;

class Timer {
//...

但是...根据 php.net

Fully qualified names (i.e. names starting with a backslash) are not allowed in namespace declarations, because such constructs are interpreted as relative namespace expressions.


所以这似乎不是正确的选择......(我的IDE也在它下面给了我一个愤怒的波浪线......)
除了回到另一个版本的 Composer ,我该如何解决这个问题?
文件结构:
C:\udvikling\MyProjectRoot -- the root of the project
C:\udvikling\MyProjectRoot\src --the php sources
C:\udvikling\MyProjectRoot\src\MyProjectClasses -- my PHP classes
C:\udvikling\MyProjectRoot\src\vendor -- my vendor stuff for composer
C:\udvikling\MyProjectRoot\src\composer.json - my composer config

最佳答案

我去做了一些 extra reading .
显然 psr-4 的目标之一是减少文件夹深度。

The “src” and “tests” directories have to include vendor and package directory names. >This is an artifact of PSR-0 compliance.


Many find this structure to be deeper and more repetitive than necessary. This proposal suggests that an additional or superseding PSR would be useful [...]


It’s difficult to implement package-oriented autoloading via an extension or amendment to PSR-0, because PSR-0 does not allow for an intercessory path between any portions of the class name.


所以基本上,问题出在我的 MyProjectClasses 里面。目录,用于类 MyProject\Core\Timer我把它放在一个类似的目录结构中: C:\udvikling\MyProjectRoot\src\MyProjectClasses\MyProject\Core\Timer.php .
这必须更改为不包含基本包: C:\udvikling\MyProjectRoot\src\MyProjectClasses\Core\Timer.php

关于php - composer dump-autoload -o 跳过我所有的类(class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65332187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com