• php register_shutdown_function


    register_shutdown_function — Register a function for execution on shutdown

    void register_shutdown_function ( callable $callback [, mixed $parameter [, mixed $... ]] )

    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.

    脚本时常死掉,而且并不总是那么好看. 我们可不想给用户显示一个致命错误,又或者一个空白页(在display_errors设为off的情况下) . PHP中有一个叫做  register_shutdown_function 的函数,可以让我们设置一个当执行关闭时可以被调用的另一个函数.也就是说当我们的脚本执行完成或意外死掉导致PHP执行即将关闭时,我们的这个函数将会 被调用.所以,我们可以使用在脚本开始处设置一个变量为false,然后在脚本末尾将之设置为true的方法,让PHP关闭回调函数检查脚本完成与否. 如果我们的变量仍旧是false,我们就知道脚本的最后一行没有执行,因此它肯定在程序执行到某处死掉了.我准备了一个非常基本的例子,可以演示在一个致 命错误需要显示时,你应该怎么给用户一些合适的反馈.你可以通过关闭致命错误的显示(译注:可以设置display_errors和 error_reporting),让例子看起来好看些.

    <?php 
    $clean = false; 
    function shutdown_func(){ 
    global $clean; 
    if (!$clean){ 
    die("not a clean shutdown"); 

    return false; 


    register_shutdown_function("shutdown_func"); 

    $a = 1; 
    $a = new FooClass(); // 将因为致命错误而失败
    $clean = true; 

    ?>

    正如你所看到,如果关闭回调函数运行时,clean变量没有被设为true,shutdown_func函数将会打印出一些东西.这个东西可以包装成一个类(不使用全局变量).

    PHP提供register_shutdown_function()这个函数,能够在脚本终止前回调注册的函数,也就是当 PHP 程序执行完成后执行的函数。

    php程序员站

    例子:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    <title>register_shutdown_function示例</title> 
    </head> 
    <body> 
    <?php 
    $starttime=microtime(true); 
    www.phperz.com 
    function Test(){ 
    $starttime=microtime(true);    
    if(!file_exists('Test.txt')){     //判断如果文件不存在!! 
    $Str = fopen('Test.txt',"w+");        
    fwrite($Str,'我是在最后写进来的.时间:$starttime'); 
    fclose($Str); 
    echo "创建完成!创建时间:$starttime"; 

    else { //如果存在; 
    echo '文件已经存在'; 


    register_shutdown_function('Test'); 
    echo "程序开始:".$starttime."<br>"; phperz~com 

    for($i=0;$i<1000;$i++){ 
    echo "Echo<br/>"; 

    exit; 
    ?> 
    </body> 
    </html>

    register_shutdown_function的作用是指定当本页面所有脚本执行完成之后执行的函数。
    <?php 
    function aaa() { 
    echo '创建文件'; 
    if($ttt = fopen('D:/web_root/tx.txt',"w+"))  //此处要用绝对路径,用相对路径即无效。原因请看后面的解释 

    fwrite($ttt,'you are write after exit'); 
    fclose($ttt); 



    register_shutdown_function('aaa');  // 函数名称无需带括号,用引号包住即可。 当本页面所有语句都执行完成,或者超时时aa函数。 
    exit(); 
    ?>

    register_shutdown_function 执行机制是:PHP把要调用的函数调入内存。当页面所有PHP语句都执行完成时,再调用此 函数。注意,在这个时候从内存中调用,不是从PHP页面中调用,所以上面的例子不能使用相对路径,因为PHP已经当原来的页面不存在了。就没有什么相对路 径可言。

    注意:register_shutdown_function 是指在执行完所有PHP语句后再调用函数,不要理解成客户端关闭流浏览器页面时调用函数。
    可以这样理解调用条件:
    1、当页面被用户强制停止时

    2、当程序代码运行超时时

    3、当PHP代码执行完成时

  • 相关阅读:
    [机器学习]单变量线性回归(最小二乘法)
    [机器学习]kNN进邻算法
    Python笔记(读取txt文件中的数据)
    [机器学习笔记] 1监督学习
    LeetCode(Add Two Numbers)
    缓冲区溢出在Linux虚拟机上的实现过程中的问题与解决
    数据库与后端的映射
    电子公文传输系统 团队作业(五):冲刺总结
    电子公文传输系统 团队作业(五):冲刺总结(第一天)
    电子公文传输系统 团队作业(四):描述设计
  • 原文地址:https://www.cnblogs.com/youxin/p/3949277.html
Copyright © 2020-2023  润新知