| 南墙 @Ta | |
| 2025-09-13 00:48 |
<?php
// 注册自动加载函数
spl_autoload_register(function($className) {
// $className 是“命名空间+类名”(如 app\Admin\User)
// 步骤1:把命名空间的“\”换成系统路径的“/”(适配Windows/Linux)
$filePath = str_replace('\\', DIRECTORY_SEPARATOR, $className);
// 步骤2:拼接完整文件路径(假设所有类文件都以.php结尾)
$file = __DIR__ . '/' . $filePath . '.php';
// 步骤3:如果文件存在,就加载
if (file_exists($file)) {
require $file;
}
});
// -------------- 下面直接用类,不用手动require --------------
// 用管理员User类
$adminUser = new app\Admin\User();
echo $adminUser->getRole(); // 输出:我是管理员用户
// 用普通用户User类(用use简化)
use app\Client\User;
$clientUser = new User();
echo $clientUser->getRole(); // 输出:我是普通用户| 阅:281 | 发表于2025-09-13 00:48 |
心灵鸡汤:
      
®2019-2026 all power by Zlproject3.8
川公网安备 51192202000151号
蜀ICP备2021014921号-2
首页 #top
天合之作 曲在人意