1. 程式人生 > >PHP函數register_shutdown_function的使用示例

PHP函數register_shutdown_function的使用示例

sep curl array bbs function () help 代碼 reg

某些情況下,我們需要在程序執行結束時,做一些後續的處理工作,這個時候,PHP的register_shutdown_function函數就可以幫我們來實現這個功能。
函數簡介
當php程序執行完成後,自動執行register_shutdown_function函數,該函數需要一個參數,用來指定由誰處理這些後續的工作。其中,程序執行完成,分為以下幾種情況:
第一種:php代碼執行過程中發生錯誤
第二種:php代碼順利執行成功
第三種:php代碼運行超時
第四種:頁面被用戶強制停止

程序入口文件引入

register_shutdown_function(array(‘Error‘, ‘systemError‘));

Error類源碼

<?php

class Error
{
    public static function systemError()
    {
        $message = ‘‘;
        if ($error = error_get_last()) {
            $separator = "\r\n";
            $date = date(‘Y-m-d H:i:s‘, time());
            $message .= "[".$date."]---message:" . $error[‘message‘] . $separator
; $message .= "file:" . $error[‘file‘] . $separator; $message .= "line:" . $error[‘line‘] . $separator; $url = ‘‘; $data = array( ‘info‘=>$message, ‘type‘=>‘bbs‘ ); Helper::curlRequest($url
, $data, true, 5); }else{ //此處處理其它一些業務邏輯 } } }

PHP函數register_shutdown_function的使用示例