2016-12-01 68 views
2

下一個代碼:error_get_last()在set_error_handler()後返回null; PHP 7.0

$a["x"];//should trigger notice 
var_dump(error_get_last());//return the error array 

運行完美並返回一個錯誤的數組。 但是當我使用的set_error_han dler返回null

function _do_nothing(){} 
set_error_handler('_do_nothing'); 

$a["x"];//should trigger notice 
var_dump(error_get_last());//return null 

此代碼工作完全在PHP5.4我已經更換3個月前到PHP7從那以後,我得到空在error_get_last() 我使用的是關機功能檢查如果發生錯誤,並且如果發生錯誤,則將其發送給開發人員。

  1. 有沒有辦法讓這段代碼在PHP7中運行?並使error_get_last()與set_error_handler()一起工作?
  2. 解決方法的其他想法?並使關機功能得到最後一個錯誤?我使用CodeIgniter,並且不想破解他們的內部代碼。
+0

+1提的PHP版本。這幫助我理解了爲什麼4年前我的代碼突然停止工作。 – agoldev

回答

0

這是正常的現象,因爲在你的榜樣錯誤已經由_do_nothing()函數處理,所以error_get_last()回報null

如果刪除set_error_handler('_do_nothing')錯誤將不再被處理,你會得到最後一個錯誤再次

function _do_nothing(){} 
//set_error_handler('_do_nothing'); 

$a["x"];//should trigger notice 
var_dump(error_get_last());//return array() "Undefined variable: a" 

此行爲是在PHP5一樣PHP7

+0

但是,如果在錯誤處理程序中調用error_get_last(),則會有很大差異。如果您嘗試使用自定義函數捕獲錯誤(例如解析erorrs),例如在函數error_get_last()中調用'_do_nothing()'並調用'error_get_last()'將返回PHP 5中的錯誤。但是,在PHP 7中*它會返回NULL。 – agoldev

+0

好吧,它更復雜。實際上,PHP 5和7的不同行爲需要註冊一個自定義異常處理程序。我記錄在這裏:https://stackoverflow.com/questions/47728546/error-get-last-returns-null-in-php-7-when-a-custom-exception-handler-is-set – agoldev