标题本身就说明了一切。这是我的项目结构:
|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();
or
use Database\Core\MySQL;
$mysql = new MySQL();
See 使用命名空间:别名/导入
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)