作曲家PSR-4自动加载完全忽略

问题描述 投票:-1回答:2

我有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

请帮忙,谢谢。

php composer-php autoloader psr-4
2个回答
0
投票

您的命名空间被称为Song,但是你把它称为Songs


0
投票

RandomSong使用Song命名空间,而您的装载机是Songs命名空间。

© www.soinside.com 2019 - 2024. All rights reserved.