registershutdownfunction php,【原创】PHP register_shutdown_function函数详解

问题背景:

PHP脚本有时会抛致命错误,怎么有效的捕获此类异常呢? 一般我们推荐使用PHP的 try...catch... 语句来捕获,

不过本文我们介绍另外一种方法:register_shutdown_function()

函数作用:

PHP中有一个 register_shutdown_function ( callback function [, mixed parameter [, mixed ...]] ) 的函数,其作用是:

1、或在脚本执行完毕后,触发通过 register_shutdown_function预先注册的回调函数。

2、或在脚本中调用了exit()、die()等终止函数时,触发通过 register_shutdown_function预先注册的回调函数。

样板代码:

function shutdownCallback()

{

echo "enter into shutdownCallback...." . PHP_EOL;

print_r(error_get_last());

}

//注册回调函数

register_shutdown_function('shutdownCallback');

//本行 new 会发生致命错误

$object = new notExistClass();

echo 'bye....' . PHP_EOL;

运行结果:

PHP Fatal error: Uncaught Error: Class 'notExistClass' not found in /path/to/demo/shutdown.php:12

Stack trace:

#0 {main}

thrown in /path/to/demo/shutdown.php on line 12

Fatal error: Uncaught Error: Class 'notExistClass' not found in /path/to/demo/shutdown.php:12

Stack trace:

#0 {main}

thrown in /path/to/demo/shutdown.php on line 12

enter into shutdownCallback....

Array

(

[type] => 1

[message] => Uncaught Error: Class 'notExistClass' not found in /path/to/demo/shutdown.php:12

Stack trace:

#0 {main}

thrown

[file] => /path/to/demo/shutdown.php

[line] => 12

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值