0

我知道这个set_error_handler函数。但我想知道是否可以为某些用户定义的函数设置自定义错误处理程序?例子。

  function ArrayToString ($Array){
    if (!is_array($Array)){
      trigger_error("Not Array", Not_Array); // I know this is invalid syntax but an example
    }
    return implode (" ",$Array);
  }

然后是这样的:

   set_handler_Not_Array(){
     //Format with some CSS
     echo "The Argument supplied within ".$errline." <br> Inside: ".$errfile." Is not a valid Array";
    exit;
   }

这样的解决方案可能吗?或者我是否必须求助于当前正在使用的错误处理:

function Error_Handler($errno, $errstr,$error_file,$error_line)
    {
        $MySQLCheck = "mysql_connect()"; // String to search for 
        $MySQL = strpos($errstr, $MySQLCheck); // Looks inside the $errstr for the string $MySQLCheck
        if ($MySQL !== false) // if Contains the $MySQLCheck
        {
            unset($errstr); // Unsets the current error message (which usually states username@host 
            $errstr = "Can Not Connect To Database"; // Changes the error message to something thatdoes not give away username or host. 
        }
        $UndefinedIndex = "Undefined index:"; // String to search for 
        $Index = strpos($errstr, $UndefinedIndex); // Searches $errstr for the String listed above 
        if ($Index !== false) // If contains the $UndefinedIndex
        {
            $errstrbkup = $errstr; // Saves $errstr to another variable before the $errstr is changes 
            $str = str_replace("Undefined index:", "", $errstrbkup); // replaces "Undefined Index:" with nothing 
            $errstr = "A Button Does not Seem To Be Handled Correctly! <br> <b>Button Name:</b> $str ";  // Changes the $errstr to a new message 
        }
        $UndefinedIndex = "Undefined variable:"; // String to search for 
        $Variable = strpos($errstr, $UndefinedIndex); // Searches $errstr for the String listed above 
        if ($Variable !== false) // If contains the $UndefinedIndex
        {
            $errstrbkup = $errstr; // Saves $errstr to another variable before the $errstr is changes 
            $str = str_replace("Undefined variable:", "", $errstrbkup); // replaces "Undefined Index:" with nothing 
            $errstr = "A Variable Does Not Seem To Be Handled. Check $str";  // Changes the $errstr to a new message 
        }   
            $ErrorFile = str_replace("/home/u394942373/public_html/", "", $error_file); // Removes the /var/www from showing in the error
        // remove the full path up to the file root if detected

        echo 
            "Opps, It Seems An error Has Been Encountered <br>
            Technical Information Listed Below: <br>
            <br><br><br>
            <b>Error Message:</b> $errstr <br>
            <b>File Causing Error:</b> $ErrorFile<br>
            <b>Line:</b> $error_line <br>
            A Member Of The Technical Department Has Been Notified And Will Fix This Error When Available.
            ";
     die();
    }
    //set error handler
    set_error_handler("Error_Handler");

这样做的原因是因为我的直线经理希望为某些类型的错误消息有一个自定义错误处理程序......或者对于抛出的每个新错误,我们仍然需要修改当前的错误处理程序

4

0 回答 0