php 说明: register_shutdown_function

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13499399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:35:40  来源:igfitidea点击:

Explanation of: register_shutdown_function

php

提问by Daryl Gill

What does register_shutdown_functiondo?

有什么作用register_shutdown_function

I have no understanding whatsoever on the usage of it and what it does. I know you can use it and fit nicely with a custom error handler, but I don't know what to pass through it, what to do with it, etc.

我对它的用法和作用一无所知。我知道您可以使用它并且非常适合自定义错误处理程序,但我不知道如何通过它,如何处理它等。

Could someone explain it in the easiest terms for this coding newbie?

有人可以用最简单的术语为这个编码新手解释一下吗?

回答by Florin Stingaciu

This is actually a pretty simple function. So you have a php script:

这实际上是一个非常简单的函数。所以你有一个 php 脚本:

<?php
  echo "Here is my little script";

  function endScript(){
       echo "My little Script has finished";
  }

  register_shutdown_function('endScript');

?>

Basically what happens is your script runs and when it's finished because we added this line register_shutdown_function('endScript');it will call the function endScript and finally output My little script has finished.

基本上发生的事情是你的脚本运行,当它完成时,因为我们添加了这一行register_shutdown_function('endScript');,它将调用函数 endScript 并最终输出My little script has finished

Here's a little extra from the documentation:

这是文档中的一些额外内容

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.

注册要在脚本执行完成或调用 exit() 后执行的回调。可以对 register_shutdown_function() 进行多次调用,并且每次调用的顺序与注册的顺序相同。如果您在一个已注册的关闭函数中调用 exit(),则处理将完全停止并且不会调用其他已注册的关闭函数。

*Update

*更新

A great use for it is when you frequently use the function called exit()in php.

当您经常使用exit()php 中调用的函数时,它的一个很好的用途。

For example:

例如:

<?php 

  function endScript(){
       echo "My little Script has finished";
  }
  register_shutdown_function('endScript');

  $result = some_intricate_function()

  if($result == 0){
     exit();
  } 

  $result = some_other_intricate_function()

  if($result == 0){
     exit();
  }

/* and this keeps reoccurring */
?>

Now notice that even though you call exit multiple times you don't have to call the endScriptfunction before exit each time. You registered it in the begining and now you know that if you ever exit or your script finishes then your function is called.

现在请注意,即使您多次调用 exit,您也不必endScript每次都在退出之前调用该函数。您在开始时注册了它,现在您知道如果您退出或脚本完成,那么您的函数将被调用。

Now of course my shutdown function is pretty useless; but it can become useful if you need to clean things (ie. Close open file handles, save some persistent data, etc)

现在我的关机功能当然没用了;但如果您需要清理东西(即关闭打开的文件句柄,保存一些持久数据等),它会变得有用

回答by FThompson

register_shutdown_functionregisters a shutdown hook, which is a function to be executed when the script shuts down. It takes a callback function as parameter, which is the function to be executed.

register_shutdown_function注册一个关闭钩子,这是一个在脚本关闭时要执行的函数。它以回调函数为参数,即要执行的函数。

I recommend that you check out the function's documentation.

我建议您查看该函数的文档