PHP register_shutdown_function()函數(shù)的使用示例
通過 register_shutdown_function 方法,可以讓我們設(shè)置一個當(dāng)執(zhí)行關(guān)閉時可以被調(diào)用的另一個函數(shù)。
也就是說,當(dāng)我們的腳本執(zhí)行完成或者意外死掉導(dǎo)致 php 執(zhí)行即將關(guān)閉時,我們的這個函數(shù)會被調(diào)用。
【使用場景】
① 頁面被(用戶)強(qiáng)制停止
② 程序代碼意外終止或超時
③ 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'));
//錯誤測試
echo md6();
執(zhí)行,輸出:
( ! ) 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é)束
D:\practise\php\Error\error.txt:
this is a test
注意:register_shutdown_function 方法是從內(nèi)存中調(diào)用的,因此在使用 file_put_contents 方法時,第一個參數(shù)一定要使用絕對路徑。
- PHP register_shutdown_function函數(shù)的深入解析
- php ignore_user_abort與register_shutdown_function 使用方法
- php中__destruct與register_shutdown_function執(zhí)行的先后順序問題
- PHP中使用register_shutdown_function函數(shù)截獲fatal error示例
- PHP錯誤處理函數(shù)register_shutdown_function使用示例
- php register_shutdown_function函數(shù)詳解
- PHP中register_shutdown_function函數(shù)的基礎(chǔ)介紹與用法詳解
相關(guān)文章
PHP使用星號替代用戶名手機(jī)和郵箱的實(shí)現(xiàn)代碼
這篇文章主要介紹了PHP使用星號替代用戶名手機(jī)和郵箱的實(shí)現(xiàn)代碼,需要的朋友可以參考下2018-02-02
phpinfo 系統(tǒng)查看參數(shù)函數(shù)代碼
并根據(jù)自身的理解做了很多修改和優(yōu)化,就當(dāng)前而言,這是探測信息最全面的PHP探針了!2009-06-06
laravel 實(shí)現(xiàn)用戶登錄注銷并限制功能
今天小編就為大家分享一篇laravel 實(shí)現(xiàn)用戶登錄注銷并限制功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

