我正在为一个简单的网站创建自己的MVC框架,我想使用名称空间,但我不知道如何自动加载它们。以下是我迄今为止尝试的内容:
我有一个
init.php
具有自动加载功能的文件:
spl_autoload_register(function($class){
$file = __DIR__ . '/../' . str_replace('\\', '/', $class) .'.php';
echo $file;
if (file_exists($file)) {
require_once $file;
echo 'yes';
}
use App\Core\App as App;
$app = new App;
D:\xampp\htdocs\MVC\App/../App/Core/App.phpyes
Fatal error: Class 'App\Core\App' not found in D:\xampp\htdocs\MVC\App\init.php on line 28
第28行是
$app = new App;
以下是我如何命名我的应用程序文件:
<?php
namespace App\Core\App;
class App
{
.....
文件结构如下:
| App
|--| Core
|--|--| App.php
...
| init.php
知道为什么吗?