欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php register_shutdown_function函數(shù)詳解

 更新時間:2017年07月23日 23:05:58   作者:北京流浪兒  
register_shutdown_function() 函數(shù)可實現(xiàn)當程序執(zhí)行完成后執(zhí)行的函數(shù),其功能為可實現(xiàn)程序執(zhí)行完成的后續(xù)操作,需要的朋友可以參考下

設(shè)定錯誤和異常處理三函數(shù)

register_shutdown_function(array(‘Debug','fatalError')); //定義PHP程序執(zhí)行完成后執(zhí)行的函數(shù)
set_error_handler(array(‘Debug','appError')); // 設(shè)置一個用戶定義的錯誤處理函數(shù)
set_exception_handler(array(‘Debug','appException')); //自定義異常處理。

功能:register_shutdown_function() 函數(shù)可實現(xiàn)當程序執(zhí)行完成后執(zhí)行的函數(shù),其功能為可實現(xiàn)程序執(zhí)行完成的后續(xù)操作。程序在運行的時候可能存在執(zhí)行超時,或強制關(guān)閉等情況,但這種情況下默認的提示是非常不友好的,如果使用register_shutdown_function()函數(shù)捕獲異常,就能提供更加友好的錯誤展示方式,同時可以實現(xiàn)一些功能的后續(xù)操作,如執(zhí)行完成后的臨時數(shù)據(jù)清理,包括臨時文件等。

可以這樣理解調(diào)用條件:

1、當頁面被用戶強制停止時

2、當程序代碼運行超時時

3、當PHP代碼執(zhí)行完成時,代碼執(zhí)行存在異常和錯誤、警告

example1:

<?php
function test() {
  echo "test()";
}
register_shutdown_function("test");
echo "show: ";

//將輸出
show:test()

example2:

<?php
 class ClassDemo {
     public function __construct() {
       register_shutdown_function(array($this, "f"));
     }
  
     public function f() {
       echo "f()";
     }
   }
 
   $demo = new ClassDemo();
   echo "before </br>";

//將輸出
before
f()

example3:

<?php
function f($str) {
    echo $str."<br>";
}
 
register_shutdown_function("f","hello");

  class ClassDemo {
    public function __construct() {
      register_shutdown_function(array($this, "f"),"hello");
    }
 
    public function f($str) {
      echo "f():".$str;
    }
  }

$demo = new ClassDemo();
echo "before </br>";

//將輸出
before
hello
f():hello

注意事項

1,register_shutdown_function()函數(shù)可重復(fù)調(diào)用,但執(zhí)行的順序與注冊的順序相同
2,如果在調(diào)用register_shutdown_function()函數(shù)之前有exit()函數(shù)調(diào)用,register_shutdown_function()函數(shù)將不能執(zhí)行
3,PHP4后支持注冊函數(shù)參數(shù)傳遞
4,在某些服務(wù)端,如Apache,當前目錄在register_shutdown_function()函數(shù)中能夠改變
5,register_shutdown_function()函數(shù)執(zhí)行在headers發(fā)送之后

相關(guān)文章

最新評論