2012-05-22 74 views
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); 

是,使用時,顯示的問題罷了。它被跳過我認爲但不確定。我無法訪問服務器的錯誤日誌,因此我不知道是否正在生成錯誤,但代碼將貫穿到我主代碼中的退出點(與課程無關)

- -EDIT--

用$ this-> Log更改$ Log的答案已經解決了$ Log變量的問題。它仍然沒有解決在foreach循環中顯示錯誤代碼數組的問題。

- 編輯 -

我加入這個 - $>錯誤代碼的foreach循環解決的問題。

+0

您正在爲此寫什麼PHP版本? – PeeHaa

+0

@RepWhoringPeeHaa:我不確定確切的版本,但它是PHP 5.服務器由netfirms.ca在Linux上使用Apache(不確定味道) – RMDan

+0

爲什麼使用'var'關鍵字? – PeeHaa

回答

1

您需要訪問$this->Log()中的類變量LogClear_Log()函數。

嘗試:

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

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

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

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

這有助於我的問題的一部分。謝謝。 – RMDan

+0

這是有用的,但不是完整的答案。如果沒有其他人幫助解決問題,我會接受這個答案。再次感謝解決方案。 – RMDan

0

您應修改$this->Log的方法LogClear_Log,就像你在Display_Log訪問它。而你只需訪問一個局部變量。

0

您正在使用名稱Log同時具有函數和變量。嘗試更改任何一個名稱。