Home > Backend Development > PHP Tutorial > The specific application of register_shutdown_function function in php (including detailed explanation)

The specific application of register_shutdown_function function in php (including detailed explanation)

烟雨青岚
Release: 2023-04-08 19:36:02
forward
2682 people have browsed it

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 = &#39;&#39;;
if ($error = error_get_last()) {
//程序报错处理,通常会跳转到用户自定义的页面,同时记录错误信息
$separator = "\r\n";
$message .= "错误:" . $error[&#39;message&#39;] . $separator;
$message .= "文件:" . $error[&#39;file&#39;] . $separator;
$message .= "行数:" . $error[&#39;line&#39;] . $separator;
$message = str_replace($separator, &#39;<br />&#39;, $message);
header(&#39;Location:http://&#39;.$_SERVER[&#39;HTTP_HOST&#39;].&#39;/error.php&#39;);
exit;
}else{
//此处处理其它一些业务逻辑
}
}
}
Copy after login

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 &#39;CustomHandle.php&#39;;
register_shutdown_function(array(&#39;com\antp\CustomHandle&#39;,&#39;systemError&#39;));
Copy after login

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!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template