溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP中全局錯(cuò)誤處理的示例分析

發(fā)布時(shí)間:2021-08-31 11:50:17 來(lái)源:億速云 閱讀:158 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了PHP中全局錯(cuò)誤處理的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

目的

PHP的全局錯(cuò)誤處理,在開(kāi)發(fā)項(xiàng)目的時(shí)候很有用,可以幫助開(kāi)發(fā)者快速定位一些問(wèn)題,提高工作效率。默認(rèn)情況下,全局錯(cuò)誤會(huì)直接輸出,但是最近開(kāi)發(fā)時(shí)使用的一個(gè)框架庫(kù)對(duì)全局錯(cuò)誤處理進(jìn)行了設(shè)定,導(dǎo)致很多錯(cuò)誤信息沒(méi)有輸出,在定位問(wèn)題上有一定的耗時(shí)。所以,研究了一下此庫(kù)的實(shí)現(xiàn),發(fā)現(xiàn)它設(shè)定了error_reporting和set_error_handler,導(dǎo)致此現(xiàn)象?,F(xiàn)在記錄一下這兩個(gè)函數(shù)的用法,作為備忘錄。

背景

PHP沒(méi)有類(lèi)型檢測(cè),開(kāi)發(fā)人員比較容易輸入錯(cuò)誤單詞,引起致命錯(cuò)誤,最終導(dǎo)致腳本停止執(zhí)行。如果這個(gè)時(shí)候,沒(méi)有得到任何錯(cuò)誤消息,那么會(huì)是一件很痛苦的事情。你不得不從腳本的第一行代碼開(kāi)始調(diào)試,在成千上萬(wàn)行的代碼中不斷的print或者echo,直到定位到這個(gè)輸錯(cuò)的單詞。然后,有不得不原路返回,將先前添加的print或echo全部刪除。這時(shí)一件及其枯燥乏味的工作。

一般情況

正常情況下,php會(huì)將致命錯(cuò)誤直接輸出,會(huì)將錯(cuò)誤的出處(文件地址,行號(hào))和原因等輸出,這樣,開(kāi)發(fā)著可以很方便的定位到問(wèn)題。

但是有些時(shí)候,可能由于php.ini的設(shè)置問(wèn)題,可能是第三方框架配置的問(wèn)題,導(dǎo)致這些信息沒(méi)有輸出,那么此時(shí),必須學(xué)會(huì)自己設(shè)置相關(guān)參數(shù),輸出這些錯(cuò)誤信息,幫助快速定位問(wèn)題。

error_reporting

error_reporting是一個(gè)php的全局配置參數(shù),在php.ini中。用于配置錯(cuò)誤輸出級(jí)別,參數(shù)是比特位,可以用來(lái)設(shè)置錯(cuò)誤輸出的級(jí)別,下面是從php.ini中copy出來(lái)的信息:

; error_reporting is a bit-field. Or each number up to get desired error
; reporting level
; E_ALL - All errors and warnings (doesn't include E_STRICT)
; E_ERROR - fatal run-time errors
; E_RECOVERABLE_ERROR - almost fatal run-time errors
; E_WARNING - run-time warnings (non-fatal errors)
; E_PARSE - compile-time parse errors
; E_NOTICE - run-time notices (these are warnings which often result
; from a bug in your code, but it's possible that it was
; intentional (e.g., using an uninitialized variable and
; relying on the fact it's automatically initialized to an
; empty string)
; E_STRICT - run-time notices, enable to have PHP suggest changes
; to your code which will ensure the best interoperability
; and forward compatibility of your code
; E_CORE_ERROR - fatal errors that occur during PHP's initial startup
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's
; initial startup
; E_COMPILE_ERROR - fatal compile-time errors
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors)
; E_USER_ERROR - user-generated error message
; E_USER_WARNING - user-generated warning message
; E_USER_NOTICE - user-generated notice message
;
; Examples:
;
; - Show all errors, except for notices and coding standards warnings
;
;error_reporting = E_ALL & ~E_NOTICE
;
; - Show all errors, except for notices
;
;error_reporting = E_ALL & ~E_NOTICE | E_STRICT
;
; - Show only errors
;
;error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
;
; - Show all errors except for notices and coding standards warnings
;
error_reporting = E_ALL & ~E_NOTICE

默認(rèn)情況下,php會(huì)輸出所有錯(cuò)誤信息,除了notice。同樣,php標(biāo)準(zhǔn)函數(shù)中提供了名稱相同的函數(shù)error_reporting(int $level),用于在php腳本中,完成同樣的功能。這樣將不會(huì)影響其他程序。值得注意的是,$level為0的時(shí)候是關(guān)閉錯(cuò)誤輸出,也就是任何錯(cuò)誤都不會(huì)輸出。

set_error_handler

php的默認(rèn)錯(cuò)誤處理是將消息輸出。但是,有時(shí)候需要定義一些其他操作,這時(shí)就需要自定義錯(cuò)誤處理函數(shù)。php提供內(nèi)置函數(shù)set_error_handler可以幫助我們注冊(cè)自己的錯(cuò)誤處理函數(shù)。函數(shù)原型如下:

mixed set_error_handler ( callback $error_handler [, int $error_types = E_ALL | E_STRICT ] )

值得注意的是,即使注冊(cè)了錯(cuò)誤處理函數(shù),默認(rèn)的行為仍然會(huì)執(zhí)行,也就是錯(cuò)誤出現(xiàn)時(shí),仍然會(huì)輸出錯(cuò)誤信息,所以需要在程序中顯示的將錯(cuò)誤級(jí)別設(shè)置為0,然后在注冊(cè)自己的的錯(cuò)誤處理函數(shù)。這種方式,在生產(chǎn)環(huán)境下,尤其重要,因?yàn)榧磿r(shí)出錯(cuò),敏感內(nèi)部錯(cuò)誤信息也不會(huì)暴露給潛在的惡意用戶。還有很重要的一點(diǎn)需要指出,自定義錯(cuò)誤處理函數(shù)不能處理fatal error(比如編譯錯(cuò)誤)。下面是一個(gè)使用自定義錯(cuò)誤處理函數(shù)的列子:

<?php
error_reporting (0);
function error_handler ($error_level, $error_message, $file, $line) {
  $EXIT = FALSE;
  switch ($error_level) {
    case E_NOTICE:
    case E_USER_NOTICE:
      $error_type = 'Notice';
      break;
    case E_WARNING:
    case E_USER_WARNING:
      $error_type = 'Warning';
      break;
    case E_ERROR:
    case E_USER_ERROR:
      $error_type = 'Fatal Error';
      $EXIT = TRUE;
      break;
    default:
      $error_type = 'Unknown';
      $EXIT = TRUE;
      break;
  }
  printf ("%s: %s in %s on line %d\n", $error_type, $error_message, $file, $line);
 
  if ($EXIT) {
    die();
  }
}
set_error_handler ('error_handler');
 
//new NonExist();
echo $novar;
echo 3/0;
trigger_error ('Trigger a fatal error', E_USER_ERROR);
new NonExist();
?>

執(zhí)行此腳本可以得到下面的輸出:

Notice: Undefined variable: novar in /your/php_demo_file.php on line 40

Warning: Division by zero in /your/php_demo_file.php on line 41

Fatal Error: Trigger a fatal error in /your/php_demo_file.php on line 42

可以看到,最后的“new NoExistClass()”的異常,沒(méi)有被自定義的錯(cuò)誤處理函數(shù)捕獲。

最后,捎帶提一下,set_exception_handler注冊(cè)頂層的異常處理,在web一用中,可以設(shè)定一下,然后統(tǒng)一的跳轉(zhuǎn)到錯(cuò)誤處理頁(yè)面。

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“PHP中全局錯(cuò)誤處理的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI