标题不言自明。所以这是我的项目结构:
|src
|Database
|Core
|MySQL.php
|Support
start.php
|vendor
composer.json
index.php
MySQL.php 文件:
<?php
namespace Database\Core;
//Some methods here
index.php 和 start.php 文件:
//start.php file
<?php
require __DIR__ . '/../vendor/autoload.php';
?>
//index.php file
<?php
use Database\Core;
require __DIR__ . '/src/start.php';
$mysql = new MySQL(); // Gets exception Class 'MySQL' cannot found etc.
?>
最后是我的 composer.json 自动加载部分:
"autoload": {
"psr-4": "Database\\": "src/" // Also tried "src/Database" too
}
问题出在哪里?我真的厌倦了尝试应对这种情况。请大家帮忙!谢谢。
最佳答案
初始化类时需要包含命名空间:
$mysql = new Database\Core\MySQL();
或
use Database\Core\MySQL;
$mysql = new MySQL();
关于未找到 PHP Composer Autoloader 类异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554364/