register_shutdown_function函数在php中具体应用(含详解)
发布:smiling 来源: PHP粉丝网 添加日期:2022-06-22 08:29:58 浏览: 评论:0
某些情况下,我们需要在程序执行结束时,做一些后续的处理工作,这个时候,php的register_shutdown_function函数就可以帮我们来实现这个功能。
一 register_shutdown_function函数简介
当PHP程序执行完成后,自动执行register_shutdown_function函数,该函数需要一个参数,用来指定由谁处理这些后续的工作。其中,程序执行完成,分为以下几种情况:
⑴ php代码执行过程中发生错误
⑵ php代码顺利执行成功
⑶ php代码运行超时
⑷ 页面被用户强制停止
二 register_shutdown_function函数的使用步骤
register_shutdown_function函数的使用非常简单,最多2步即可。
1. 自定义一个php类,名字为CustomHandle.php,内如如下:
- <?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. 引入注册函数
在程序入口处,引入CustomHandle.php文件,同时,注册register_shutdown_function函数,如下:
require 'CustomHandle.php';
register_shutdown_function(array('com\antp\CustomHandle','systemError'));
此时,不管你的php代码执行是否成功,最后都会CustomHandle类中的systemError方法。
Tags: register_shutdown_function
- 上一篇:构造函数在php中的使用方法(附示例)
- 下一篇:最后一页
相关文章
- ·PHP中使用register_shutdown_function函数截获fatal error示例(2021-05-22)
- ·PHP register_shutdown_function()函数的使用示例(2021-06-03)
- ·PHP中register_shutdown_function函数的基础介绍与用法详解(2021-08-21)
- ·用register_shutdown_function函数记录php的输出日志(2022-06-04)

推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)