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

PHP register_shutdown_function()函數(shù)的使用示例

 更新時(shí)間:2015年06月23日 11:42:47   投稿:junjie  
這篇文章主要介紹了PHP register_shutdown_function()函數(shù)的使用示例,當(dāng)我們的腳本執(zhí)行完成或者意外死掉導(dǎo)致 php 執(zhí)行即將關(guān)閉時(shí),register_shutdown_function()這個(gè)函數(shù)會(huì)被調(diào)用,需要的朋友可以參考下

通過 register_shutdown_function 方法,可以讓我們設(shè)置一個(gè)當(dāng)執(zhí)行關(guān)閉時(shí)可以被調(diào)用的另一個(gè)函數(shù)。

也就是說,當(dāng)我們的腳本執(zhí)行完成或者意外死掉導(dǎo)致 php 執(zhí)行即將關(guān)閉時(shí),我們的這個(gè)函數(shù)會(huì)被調(diào)用。

【使用場景】

① 頁面被(用戶)強(qiáng)制停止

② 程序代碼意外終止或超時(shí)

③ php4 中沒有析構(gòu)函數(shù),可以使用該函數(shù)模擬析構(gòu)函數(shù)

shutdown.php

<?php
header("content-type:text/html;charset=utf-8");
class Shutdown{
  public function endScript(){
    if(error_get_last()){
      echo '<pre>';
      print_r(error_get_last());
      echo '</pre>';
    }
    file_put_contents('D:\practise\php\Error\error.txt', 'this is a test');
    die('腳本結(jié)束');
  } 
}

register_shutdown_function(array(new Shutdown(), 'endScript'));

//錯(cuò)誤測試
echo md6();

執(zhí)行,輸出:

復(fù)制代碼 代碼如下:

( ! ) Fatal error: Call to undefined function md6() in D:\practise\php\Error\shutdown.php on line 18

 Array
(
    [type] => 1
    [message] => Call to undefined function md6()
    [file] => D:\practise\php\Error\shutdown.php
    [line] => 18
)
腳本結(jié)束

復(fù)制代碼 代碼如下:

D:\practise\php\Error\error.txt:
this is a test

注意:register_shutdown_function 方法是從內(nèi)存中調(diào)用的,因此在使用 file_put_contents 方法時(shí),第一個(gè)參數(shù)一定要使用絕對路徑。

相關(guān)文章

最新評論