可能是微不足道的但是我有基本自动加载的问题。我想创建沙箱项目只是为了测试新的解决方案,所以我创建了以下结构:
Sandbox
|- index.php
|- composer.json
|- vendor
| |- {autogenerated content}
|- src
|- Working.php
文件composer.json
如下所示:
{
"name": "vendor/sandbox",
"authors": [
{
"name": "foo",
"email": "bar@example.com"
}
],
"require": {
"phpunit/phpunit": "dev-master",
"phpunit/phpunit-mock-objects": "dev-master"
},
"psr-4": {
"Sandbox\\": "src/"
}
}
当然,我在更改后运行composer.update
。然后我写了一个简单的Working.php
:
<?php
namespace Sandbox;
class Working
{
public function __construct() {
echo "Hello World";
}
}
当然还有index.php
:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Sandbox\Working;
new Working();
?>
我检查了这些文件的权限只是为了确定,但是当我尝试运行时,我得到了
PHP Fatal error: Class 'Sandbox\Working' not found in /var/www/Sandbox/index.php on line 6
我意识到这可能是微不足道的,但这里可能出现什么问题?
答案 0 :(得分:1)
在您的composer.json
,您缺少autoload
键。
它应该像
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
}
答案 1 :(得分:1)
我相信
"psr-4": {
"Sandbox\\": "src/"
}
应该是:
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
所以你的文件是:
{
"name": "vendor/sandbox",
"authors": [
{
"name": "foo",
"email": "bar@example.com"
}
],
"require": {
"phpunit/phpunit": "dev-master",
"phpunit/phpunit-mock-objects": "dev-master"
},
"autoload": {
"psr-4": {
"Sandbox\\": "src/"
}
}