1

我一直在尝试编写一个用于显示错误和错误日志的类。

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 解决了这个问题。

4

3 回答 3

1

您需要访问类变量,例如$this->Log()inLogClear_Log()函数。

尝试:

public function Log($Comment, $Detail = ""){

    $this->Log .= $Comment . "\n";

    if ($Detail !== ""){
        $this->Log .= "--" . $Detail . "--\n";
    }
}

public function Clear_Log(){
    $this->Log = "";
}
于 2012-05-22T17:00:29.383 回答
0

您应该修改$this->Login 方法LogClear_Log,就像您在Display_Log. 你只需访问一个局部变量。

于 2012-05-22T17:02:27.253 回答
0

您对函数和变量都使用名称 Log。尝试更改任何人的名称。

于 2012-05-22T17:05:04.227 回答