自定义类不显示回声

时间:2012-05-22 16:58:17

标签: php

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

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-&gt;更改$ Log的答案已经修复了$ Log变量的问题。它仍然没有修复在foreach循环中显示的错误代码数组的问题。

- 编辑 -

我通过将$ this-&gt; ErrorCode添加到foreach循环来解决了这个问题。

3 个答案:

答案 0 :(得分:1)

您需要访问$this->Log()Log函数中的Clear_Log()类变量。

尝试:

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

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

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

public function Clear_Log(){
    $this->Log = "";
}

答案 1 :(得分:0)

您应该修改方法$this->LogLog中的Clear_Log,就像您在Display_Log中访问它一样。你只需要访问一个局部变量。

答案 2 :(得分:0)

你使用名称Log作为函数和变量。尝试更改任何一个的名称。