批改状态:合格
老师批语:这个参数就是类名称, 这是回调参数的规定, 没什么要理解,记住就可以
代码:
<?php//命名空间:ns1namespace ns1;class demo{public function index(){return "这是命名空间ns1中的类demo中的方法index";}}//命名空间:ns3\ns2namespace ns3\ns2;class demo{public static function index(){return "这是命名空间ns3\\ns2中的类demo中的方法index";}}//命名空间:ns3namespace ns3;class demo{public static function index(){return "这是命名空间ns3中的类demo中的方法index";}}// \ns1\demo:完全限定名称类名,以"\"开头,类似绝对路径echo "完全限定名称访问:".(new \ns1\demo)->index();echo "<hr>";// ns2\demo:限定名称类名,不以"\"开头,并带有空间名称,类似行动对路径echo "限定名称访问:".(new ns2\demo)->index();echo "<hr>";//直接访问本空间成员echo "非限定名称访问:".(new demo)->index();echo "<hr>";

代码部分:
<?phptry {spl_autoload_register(function($class){// DIRECTORY_SEPARATOR:可随操作系统不同,使用不同的目录分隔符$path = str_replace("\\",DIRECTORY_SEPARATOR,$class);$file = __DIR__.DIRECTORY_SEPARATOR.$path.".php";//is_file():判断是否是文件,返回布尔值//file_exists():判断文件是否是存在,返回布尔值if(!(is_file($file) && file_exists($file))){exit("文件不存在:".$file);}require $file;});} catch (Exception $e) {exit($e ->getMessage());}// use admin\user\auth\Index1 as index1;echo "类的自动加载:".index1::class."<br>";echo "类的自动加载的方法:".(new \admin\user\auth\Index1)->test();
编辑器文件截图:

前端截图:

总结:
1.不清楚加载器怎么实现函数的自动加载;
2.不太理解spl_autoload_register(function($class){}中$class这个参数
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号