我正在尝试使用psr-4
和作曲家实现自动加载功能但是我收到此错误,即使我已经在索引中要求它。请参阅以下错误和代码:
错误
致命错误:第7行的C:\ wamp \ www \ blog-oop \ index.php中找不到“Blog \ Classes \ Database \ Connection”类
代码
composer.json
{
"autoload": {
"psr-4": {
"Blog\\": "app/classes/Database"
}
}
}
Connection.php
<?php
namespace Blog\Classes\Database;
class Connection{
}
的index.php
<?php
require "vendor/autoload.php";
use Blog\Classes\Database\Connection;
$connection = new Connection;
结构的
>app
>classes
>Database
>Connection.php
答案 0 :(得分:1)
在你的composer.json
中"Blog\\": "app/classes/Database"
应该是
"Blog\\": "app"
因为命名空间充当查找类
的路径答案 1 :(得分:0)
我认为作曲家json中出现错误
而不是
"Blog\\": "app/classes/Database"
应该是
"Blog\\": "app/"
我的composer.json测试
{
"name": "test",
"description": "Test",
"autoload": {
"psr-4": {
"Blog\\": "app/"
}
}
}
应用程序/类别/数据库/ Connection.php
<?php
namespace Blog\Classes\Database;
class Connection {
public function __construct()
{
print_r('Connection class was called');
}
}
结果:
Connection class was called