批改状态:合格
老师批语:
实现自动加载,应该遵循如下约定:
use 默认必须是”完全限定名称”,所以"\"全局空间标识符可以不写$path = str_replace('\\', DIRECTORY_SEPARATOR, $class);require __DIR__ . DIRECTORY_SEPARATOR. $class.'.php';
<?phpnamespace index;// 引入自动加载require 'auto.php';use inc\con1;use inc\con2;use inc\con3;echo con1::index() . '<hr>';echo con2::index() . '<hr>';echo con3::index() . '<hr>';
<?php// 自动加载spl_autoload_register(function ($class) {$dir = str_replace('\\', DIRECTORY_SEPARATOR, $class);require __DIR__ . DIRECTORY_SEPARATOR . $dir . '.php';});
<?phpnamespace inc;class con1{public static function index(){return __CLASS__ . '类<br>' . __METHOD__ . '方法';}}
<?phpnamespace inc;class con2{public static function index(){return __CLASS__ . '类<br>' . __METHOD__ . '方法';}}
<?phpnamespace inc;class con3{public static function index(){return __CLASS__ . '类<br>' . __METHOD__ . '方法';}}

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