标题不言自明。所以这是我的项目结构:
|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();
请参阅使用名称空间:别名/导入
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句