文件结构如下:
index.php
composer.json
composer.lock
-lib
--books
---SearchClient.php (contains class books\SearchClient
以下docker-compose.yml + PHP适用于Windows:
{
"name" : "keithdavis/books",
"description" : "",
"version" : "0.1",
"minimum-stability": "beta",
"require" : {
"ext-curl" : "*",
"ext-json" : "*",
"guzzlehttp/guzzle": "^6.3"
},
"autoload" : {
"psr-4": {
"books\\": "lib"
}
}
}
<?php
use books\SearchClient;
require_once __DIR__."/vendor/autoload.php";
$oSearchClient = new SearchClient();
但是,在macOS(Mojave)上:
( ! ) Fatal error: Uncaught Error: Class 'books\SearchClient' not found in /var/www/html/search.php on line 6
( ! ) Error: Class 'books\SearchClient' not found in /var/www/html/search.php on line 6
Call Stack
# Time Memory Function Location
1 0.0032 416944 {main}( ) .../search.php:0
在调试中,似乎路径“lib”是问题,但我尝试过“/ lib”,但这也不起作用。我想也许我的Composer安装不正确。我是Windows上经验丰富的PHP开发人员,但对Mac来说相对较新,所以我不是100%关于在Mac上安装Composer的最佳方式。我全球遵循这些指示:
查看您的文件结构,搜索客户端类的命名空间需要是namespace books\books;
这是因为您在lib文件夹上设置命名空间书。您可以通过添加将自动装带器直接指向书籍文件夹,而不是在命名空间中使用两次书籍
{
"name" : "keithdavis/books",
"description" : "",
"version" : "0.1",
"minimum-stability": "beta",
"require" : {
"ext-curl" : "*",
"ext-json" : "*",
"guzzlehttp/guzzle": "^6.3"
},
"autoload" : {
"psr-4": {
"books\\": "lib/books"
}
}
}
确保在更改自动加载时运行composer dumpautoload
,以便重建名称空间。
希望这可以帮助。