2013-11-04 49 views
2

我正在嘗試爲php編寫一個錯誤處理程序類。我測試過該對象已經創建,但它似乎並沒有處理錯誤。錯誤處理PHP類

我在索引文件的函數中最初使用了相同的代碼,它工作正常,但我寧願有一個類。爲什麼這不處理錯誤?

class class_error 
{ 
    public function __construct() 
    { 
     // set to the user defined error handler 
     set_error_handler($this->errorHandler()); 
    } 

    public function errorHandler($errno, $errstr, $errfile, $errline) 
    { 
     //don't display error if no error number 
     if (!(error_reporting() & $errno)) { 
      return; 
     } 

     //display errors according to the error number 
     switch ($errno) 
     { 
      case E_USER_ERROR: 
       echo "<b>ERROR</b> [$errno] $errstr<br />\n"; 
       echo " Fatal error on line $errline in file $errfile"; 
       echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n"; 
       echo "Aborting...<br />\n"; 
       exit(1); 
       break; 

      case E_USER_WARNING: 
       echo "<b>WARNING</b> [$errno] $errstr<br />\n"; 
       break; 

      case E_USER_NOTICE: 
       echo "<b>NOTICE</b> [$errno] $errstr<br />\n"; 
       break; 

      default: 
       echo "<b>UNKNOWN ERROR</b> [$errno] $errstr<br />\n"; 
       break; 
     } 

     //don't execute PHP internal error handler 
     return true; 
    } 
} 
+0

夢幻般的感謝:) –

+1

增加一條,作爲一個答案。 –

+0

是的生病給你一個滴答滴答:) –

回答

3

使用此:

set_error_handler(array($this, "errorHandler")); 

更多信息:http://php.net/manual/en/language.types.callable.php

+2

如果你的PHP是最新的,使用短陣列語法:''set_error_handler([$ this,'errorHandler'])'。 –

+0

甚至不知道這存在。感謝分享 –