我一直在尝试编写一个用于显示错误和错误日志的类。
class CError {
//Error Logging
var $Log;
public function Log($Comment, $Detail = ""){
$Log .= $Comment . "\n";
if ($Detail !== ""){
$Log .= "--" . $Detail . "--\n";
}
}
public function Clear_Log(){
$Log = "";
}
public function Display_Log(){
echo $this->Log;
}
//Error Display
var $ErrorCode = array(
0 => "0: No Error Code found, so either you got here by some rogue code or ...",
1 => "1: General Error, if you are here something went wrong",
2 => "2: Invalid information provided",
3 => "3: Alternate path taken, check message for details",
42 => "42: Here is your answer, but do you know the question?",
50 => "50: You messed with the Creepers without your Diamond Sword, didn't you",
9001 => "9001: Its over 9000... or more likely a value used was to large",
418 => "418: I'm a Teapot, found the error that drove you crazy"
);
public function Error($Error = 0, $ShowLog = false){
if ($Error === ""){
$Error = 0;
}
foreach ($ErrorCode as $key => $value){
if($key == $Error){
echo "<h3 style='color:red'>" . $value . "</h3><br />";
}
}
if($ShowLog == true){
echo $this->Log;
}
}
}
这就是我使用错误类的方式
include 'CError.php';
$Error = new CError;
$Error->Log("Email is Required");
$Error->Display_Log();
$Error->Error(2,true);
问题是,使用时什么都没有显示。我想它被跳过了,但不确定。我无权访问服务器的错误日志,所以我不知道是否正在产生错误,但代码将运行到我的主代码中的退出点(与类无关)
- 编辑 -
告诉用 $this->Log 更改 $Log 的答案已经解决了 $Log 变量的问题。它仍然没有解决在 foreach 循环中显示错误代码数组的问题。
- 编辑 -
我通过在 foreach 循环中添加 $this->ErrorCode 解决了这个问题。