是的,确实,作曲家应该照顾你的依赖关系和自动加载。您的配置也是正确的。让我们看一下检查清单:
你的环境是这样的吗?
$ tree
.
├── composer.json
├── composer.phar
├── index.php
├── vendor
│ ├── autoload.php
│ └── composer
│ ├── autoload_classmap.php
│ ├── autoload_namespaces.php
│ ├── autoload_psr4.php
│ ├── autoload_real.php
│ ├── autoload_static.php
│ ├── ClassLoader.php
│ ├── installed.json
│ └── LICENSE
└── wp-content
└── plugins
└── example-plugin
└── Classes
└── MyService.php
你的composer.json 看起来像这样吗?
$ cat composer.json
{
"name": "my/project",
"authors": [
{
"name": "My Name",
"email": "[email protected] /cdn-cgi/l/email-protection"
}
],
"autoload": {
"psr-4": {
"Classes\\": "wp-content/plugins/example-plugin/Classes"
}
},
"require": {}
}
你的实现是这样的吗?
$ cat wp-content/plugins/example-plugin/Classes/MyService.php
<?php
namespace Classes;
class MyService {
}
最后,你的索引是这样的吗?
$ cat index.php
<?php
require_once('./vendor/autoload.php');
$foo = new \Classes\MyService;
var_dump($foo);
如果您对所有这些问题的回答都是“是”,那么您是否更新了自动加载器?
$ composer dump-autoload
如果你的答案是“是”,那么创建一个新目录,下载composer.phar
, run php composer.phar init
,将此答案的文件复制并粘贴到该目录中,然后重试。
如果这有效的话diff
这与您的实施有关。我的猜测是,如果您走到这一步,您的某个文件中可能隐藏着一个流浪字符(可能是 Unicode 空白)。