PHP 的 register_shutdown_function() 有时候并不会被调用或者执行

yufei       2 年, 10 月 前       730

PHP 的 register_shutdown_function() 有时候并不会被调用或者执行,这个问题出在哪里呢?

我们先来看看官方文档的解释

register_shutdown_function ( callable $callback , mixed ...$args ) : void

Registers a callback to be executed after script execution finishes or exit() is called.

Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered. If you call exit() within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.

翻译成中文就是

register_shutdown_function ( callable $callback , mixed ...$args ) : void

注册一个 callback ,它会在脚本执行完成或者 exit() 后被调用。

可以多次调用 register_shutdown_function() ,这些被注册的回调会按照他们注册时的顺序被依次调用。 如果你在注册的方法内部调用 exit(), 那么所有处理会被中止,并且其他注册的中止回调也不会再被调用。

这里面有两个坑

  1. register_shutdown_function() 的回调函数必须在 register_shutdown_function() 代码之前就已经定义声明了,否则有很大几率会出现黑天鹅事件。

  2. 第一次调用 exit() 会调用 register_shutdown_function()。 第二次调用 exit() 就会退出 register_shutdown_function() 调用链。

从某些方面说,就是 exit() 在整个运行生命周期内,只能被调用一次。

目前尚无回复
简单教程 = 简单教程,简单编程
简单教程 是一个关于技术和学习的地方
现在注册
已注册用户请 登入
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.