批改状态:合格
老师批语:
<?php
//自动加载,早期版本是使用一个名为__autoload的函数,但在php7被废弃。
//因此使用spl_autoload_register代替它。
//PHP环境:Unix系统,php8.1版本。
//介绍下自写自动加载器的目录结构:
//根文件夹,名称:a0818,里面包含‘normal’,‘test’这两个目录和一个autoloade.php文件。
//其中,‘normal’目录中包含一个user.php文件;‘test’目录包含一个hello.php文件。
//user.php为入口文件。
//下面为每个php文件具体内容。
//user.php
namespace normal;
require '../autoloade.php';
use test\hello;
$a = new hello;
echo $a->sayHello().'<br>';
echo hello::say();
//hello.php
namespace test;
class hello{
public function sayHello(){
return 'hello world';
}
public static function say(){
return'hello';
}
}
//autoload.php
//保留了编写该文件的调试过程
spl_autoload_register(function ($class){
// print_r($class);//内容为a0818\test\hello
$class = str_replace('\\',DIRECTORY_SEPARATOR,$class);
// echo __DIR__;
// die();
require __DIR__.DIRECTORY_SEPARATOR.$class.'.php';
});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号