Home >Backend Development >PHP Tutorial >The specific application of register_shutdown_function function in php (including detailed explanation)
Application of register_shutdown_function in php (including detailed explanation)
In some cases, we need to execute the program At the end, do some follow-up processing work. At this time, PHP's register_shutdown_function function can help us implement this function.
1 Introduction to the register_shutdown_function function
When the PHP program is executed, the register_shutdown_function function is automatically executed. This function requires a parameter to specify who handles these functions. follow-up work. Among them, the program execution is completed, which is divided into the following situations:
⑴ An error occurs during the execution of the php code
⑵ The php code is executed successfully
⑶ PHP code running timeout
⑷ The page was forced to stop by the user
2 Steps to use the register_shutdown_function function
The use of the register_shutdown_function function is very simple and only requires 2 steps at most.
1. Customize a php class with the name CustomHandle.php, as follows:
<?php namespace com\antp; class CustomHandle { public static function systemError() { $message = ''; if ($error = error_get_last()) { //程序报错处理,通常会跳转到用户自定义的页面,同时记录错误信息 $separator = "\r\n"; $message .= "错误:" . $error['message'] . $separator; $message .= "文件:" . $error['file'] . $separator; $message .= "行数:" . $error['line'] . $separator; $message = str_replace($separator, '<br />', $message); header('Location:http://'.$_SERVER['HTTP_HOST'].'/error.php'); exit; }else{ //此处处理其它一些业务逻辑 } } }
2. Introduce the registration function
Introduce the CustomHandle.php file at the program entrance, and at the same time, register the register_shutdown_function function, as follows:
require 'CustomHandle.php'; register_shutdown_function(array('com\antp\CustomHandle','systemError'));
At this time, regardless of whether your php code is executed successfully or not, it will eventually be in the CustomHandle class systemError method.
————————————————
Copyright statement: This article is an original article by CSDN blogger "Uncle Muyu" and follows CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/tdcqfyl/article/details/52291237
Recommended tutorial: "PHP Tutorial 》
The above is the detailed content of The specific application of register_shutdown_function function in php (including detailed explanation). For more information, please follow other related articles on the PHP Chinese website!