Composer PSR-4 Autoloading Deprecation: Class Path Mismatch
When executing composer commands like update and install, you may encounter a deprecation notice regarding a class that does not comply with the PSR-4 autoloading standard. This typically occurs when there is a discrepancy between the class's fully qualified name and the path of its corresponding file.
Path Case
The most common cause is a mismatch in the case of the pathname components and the class name. For instance, "foo/bar/Baz.php" does not correspond to "FooBarBaz." Ensure that the case of each pathname component matches the case of the namespace it represents, such as "Foo/Bar/Baz.php" for "FooBarBaz".
File Name and Class/Namespace Differences
仔细检查文件路径和命名空间之间的匹配情况。有时候,您的类(或命名空间)可能被命名为FooBar,但它的磁盘路径却是"foo-bar"。这种情况也会触发警告。您需要重命名文件或类(或命名空间)。
通常情况下,更改路径或文件比更改类或命名空间名称更容易,因为更改类或命名空间名称需要您重构代码以匹配新名称,而更改路径则不需要重构任何内容。
嵌套命名空间和缺少声明
假设您有:
"autoload": { "psr-4": { "Fizz\Buzz\": "src/" } }
并且类Dummy被定义在src/Buzz中:
// src/Buzz/Dummy.php namespace Fizz\Buzz class Dummy {}
上述代码可以正常运行,但会抛出类似其他情况的警告。正确的做法应该是:
// src/Buzz/Dummy.php namespace Fizz\Buzz\Buzz class Dummy {}
您需要不仅对受影响的类进行更改,还需要对使用或导入该类的任何其他文件进行更改(例如,通过现在声明use FizzBuzzBuzzDummy;)。
以上是为什么 Composer 会发出有关类路径不匹配的 PSR-4 自动加载弃用警告?的详细内容。更多信息请关注PHP中文网其他相关文章!