我有composer.json文件:
{
"name": "marko/art-empire",
"description": "Social network",
"type": "project",
"authors": [
{
"name": "Marko Ilic",
"email": "markowebdeveloper@gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"Songs\": "songs/"
}
}
}
autoload_psr4.php文件:
$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
'Songs\' => array($baseDir . '/songs'),
);
随机歌曲.php歌曲文件夹中的文件(位于根目录中(:
namespace Song;
class RandomSong
{
public function songName()
{
return 'Random Song';
}
}
测试.php文件:
require 'vendor/autoload.php';
use SongsRandomSong;
$randomSong = new RandomSong();
echo $randomSong->songName();
如您所见,我正在尝试自动加载随机歌曲类,但我不断得到:
Fatal error: Uncaught Error: Class 'SongsRandomSong' not found in test.php
请帮忙,谢谢。
您的命名空间称为 Song
,但您将其称为 Songs
您的RandomSong
使用Song
命名空间,而您的加载器用于Songs
命名空间。