app = $app; error_reporting(E_ALL); set_error_handler([$this, 'appError']); set_exception_handler([$this, 'appException']); // register_shutdown_function([$this, 'appShutdown']); // 移除 } } class GCValidate extends Validate { public static function cleanMaker() { static::$maker = []; } } class GCModel extends Model { public static function cleanMaker() { static::$maker = []; } } /** * 可被自动 gc 的,但需要手动调用 refClear 函数 * Class GCApp * @package tests */ class GCApp extends App { protected $initializers = [ // 覆盖父类 GCError::class, // 去掉 register_shutdown_function RegisterService::class, // 原来就有的 BootService::class, // 原来就有的 ]; /** * 添加清理函数 * @throws \Exception */ public function refClear() { $this->route->clear(); // 清理路由规则 // 清理绑定在 App 的实例 $names = []; foreach ($this->getIterator() as $name=>$_v) { $names[] = $name; } foreach ($names as $name) { $this->delete($name); } // 清理异常 handler restore_error_handler(); restore_exception_handler(); GCValidate::cleanMaker(); GCModel::cleanMaker(); } }