为什么"composer dumpautoload -o"修复了“找不到类" PHP错误? [英] Why does 'composer dumpautoload -o' fix 'Class not found' PHP error?
问题描述
我有一个Laravel 5.8
项目,该项目依赖于私有软件包.
I have a Laravel 5.8
project that is dependent on a private package.
当我运行composer install
时,该软件包已安装并显示在供应商文件夹中.
When I run composer install
the package is installed and shows up in the vendor folder.
{
...
"require": {
"php": ">=7.0",
"company/api-request": ">=1.0.0"
}
...
}
软件包src/ApiRequest.php
<?php
namespace Company;
class APIRequest
{
...
}
包composer.json
{
...
"autoload": {
"psr-4": {
"Company\\": "src/"
}
}
...
}
当我打电话给包裹时
\Company\APIRequest::run();
我得到了
Message: Class 'Company\APIRequest' not found
我知道PHP语法是正确的,因为当我运行composer dumpautoload -o
时,错误消失了,但是为什么有必要?
I know the PHP syntax is correct because when I run composer dumpautoload -o
the error is gone, but why is it necessary?
我希望composer install
或composer update
应该足够了;我对外部软件包没有任何问题.
I expect composer install
or composer update
should be sufficient; I have no problem with external packages.
我在这里错过了什么吗?
Am I missing anything here?
推荐答案
如果类名和文件名不匹配,则将导致自动加载不起作用,因为这是PSR-4的要求.从文档:
If the class name and file name don't match, that would cause the auto-loading to not work since that is a requirement with PSR-4. From the docs:
终止类名对应于以.php结尾的文件名.文件名必须与终止类名的大小写匹配.
The terminating class name corresponds to a file name ending in .php. The file name MUST match the case of the terminating class name.
如果是这种情况,composer dumpautoload -o
可能正在为您解决此问题,请参见
If that's the case, composer dumpautoload -o
is probably working around this for you, see this Reddit post:
-o起作用的原因是Composer创建了一个巨大的关联数组,其中classname = filename
The reason -o works, is Composer creates a giant associative array where classname = filename
这篇关于为什么"composer dumpautoload -o"修复了“找不到类" PHP错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!