我有composer.json文件:
{
"name": "marko/art-empire",
"description": "Social network",
"type": "project",
"authors": [
{
"name": "Marko Ilic",
"email": "[email protected]"
}
],
"require": {},
"autoload": {
"psr-4": {
"Songs\\": "songs/"
}
}
}
autoload_psr4.php文件:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Songs\\' => array($baseDir . '/songs'),
);
RandomSong.php在歌曲文件夹(这是根目录)文件:
namespace Song;
class RandomSong
{
public function songName()
{
return 'Random Song';
}
}
test.php的文件:
require 'vendor/autoload.php';
use Songs\RandomSong;
$randomSong = new RandomSong();
echo $randomSong->songName();
正如你可以看到我想要自动加载RandomSong类,但我不断收到:
Fatal error: Uncaught Error: Class 'Songs\RandomSong' not found in test.php
请帮忙,谢谢。
您的命名空间被称为Song
,但是你把它称为Songs
你RandomSong
使用Song
命名空间,而您的装载机是Songs
命名空间。