批改状态:合格
老师批语:
使用spl_autoload_register进行创建加载器
自动加载器文件autoloader.php
<?php//自动加载器// spl_autoload_registerspl_autoload_register(function($class){$p = str_replace('\\',DIRECTORY_SEPARATOR,$class);require __DIR__ . DIRECTORY_SEPARATOR . $p . '.php';});
<?phpnamespace admin\controller;class Demo1{public static function f1(){return __CLASS__ .'下的方法'. __METHOD__;}public static function getUser($name){return '我是'.$name;}}
<?phpnamespace admin\controller;class Demo2{public static function f2(){return __CLASS__ . '下的方法'.__METHOD__;}public static function getUser($name){return '我是'.$name;}}
<?php//引入加载器require './autoloader.php';//起别名use admin\controller\Demo1;use admin\controller\Demo2;//调用加载器方法echo Demo1::f1().'<hr>';echo Demo1::getUser('Demo1').'<hr>';echo Demo2::f2().'<hr>';echo Demo2::getUser('Demo2').'<hr>';

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号