批改状态:合格
老师批语:
a. 类名和文件名的相互对应(同名,类名采用大驼峰的原则,首字母大写);
b. 类空间和类文件路径的一致。
splautoloadregister(function($class){
// 将空间名和文件路径协调起来
$path = str_replace(“\“,DIRECTORY_SEPARATOR,$class);
$file = __DIR .’/‘ . $path.”.php”;
if(is_file($file) && file_exists($file)){
require $file;
}else{
die(“文件不存在”);
}
});
use admin\controller\Index;
use admin\model\User;
use admin\view\index\Hello;
文件的架构如图:
<?phpnamespace admin\controller;class Index {public static function controllerhere(){echo __FILE__;}}?>
<?phpnamespace admin\model;class User{public static function userhere(){echo __FILE__;}}?>
<?phpnamespace admin\view\index;class Hello{public static function hellohere(){echo __FILE__;}}?>
<?phpnamespace core;class Main {public function __construct(){}public static function show(){spl_autoload_register(function($class){echo $class;echo PHP_EOL;// 将空间名和文件路径协调起来$path = str_replace("\\",DIRECTORY_SEPARATOR,$class);$file = __DIR__ .'/' . $path.".php";if(is_file($file) && file_exists($file)){require $file;}else{die("文件不存在");}});}}Main::show();use admin\controller\Index;use admin\model\User;use admin\view\index\Hello;echo Index::controllerhere().PHP_EOL;echo User::userhere().PHP_EOL;echo Hello::hellohere().PHP_EOL;?>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号