我正在研究一些PHP類,可以進行一些基本的分析,調試和錯誤處理。是的,我知道XDebug,它很棒,但我想爲我的圖書館準備一些類來完成這些類型的內容。
下面是我正在處理的一個類,當出現一個錯誤消息時,它會顯示一條錯誤消息,並向我顯示它發生的行號以及該行的源代碼和X行以下和以上的行數錯誤行。類似下面這個圖片...(可能要查看圖像在新窗口/標籤看大圖)
這裏是我的類代碼我有作爲的,現在這個...
<?php
class Debugger
{
public static function debug_source($file, $line_number, $padding = 5)
{
if (!$file or !is_readable($file)) {
return false;
}
// Open the file and set the line position
$file = fopen($file, 'r');
$line = 0;
// Set the reading range
$range = array('start' => $line_number - $padding, 'end' => $line_number + $padding);
// Set the zero-padding amount for line numbers
$format = '% ' . strlen($range['end']) . 'd';
$source = '';
while (($row = fgets($file)) !== false)
{
// Increment the line number
if (++$line > $range['end'])
break;
if ($line >= $range['start']){
// Make the row safe for output
$row = htmlspecialchars($row, ENT_NOQUOTES);
// Trim whitespace and sanitize the row
$row = '<span class="number">' . sprintf($format, $line) . '</span> ' . $row;
if ($line === $line_number){
// Apply highlighting to this row
$row = '<span class="line highlight">' . $row . '</span>';
} else{
$row = '<span class="line">' . $row . '</span>';
}
// Add to the captured source
$source .= $row;
}
}
// Close the file
fclose($file);
echo '<div id="exception_error">';
echo ' <h1><span class="type"> Error [ 345 ]</span></h1>';
echo ' <div class="content">';
echo ' <p>Test error message on line number ' . $line_number. '</p>';
echo ' <p><pre class="source"><code>' . $source . '</code></pre></p>';
echo ' </div>';
echo '</div>';
}
}
// Testing the class above.
$file = 'bitmask/bitmasktest.php';
$line_number = 82;
$line_number_padding = 5;
$debugger = new Debugger();
$debugger->debug_source($file, $line_number, $line_number_padding);
?>
所以我正在尋找方法來改善和增加這個。這還不完整,這只是一個開始。
幾個基本問題。
1)我是否應該在類的頂部聲明所有變量?例如在這個類中,我訪問這樣的屬性$文件,$ line_number,$ padding。我應該聲明其在頂部(所有屬性),如...
<?php
//public/private/protected
public $this->file;
private $this->$line_number;
private $this->padding;
// etc, etc, for every single property used in a class method
// or should I access them how I currently do in
// this class like $file, $line_number, etcc????
?>
?>
2)使輸出與以上我的方法是正確的,它需要在下面這個CSS數據該頁面,我知道我應該爲它包含一個CSS文件或將其添加到我的其他css文件,但我的目標是讓這個類是自包含的,所以該類可以在任何地方運行並具有正確的輸出。我是否應該有一種方法來輸出這個類所需要的CSS,或者有什麼好的想法來保持它自我包含,並且仍然按照我想要的方式來看待(如圖中所示)?
<style type="text/css">
#exception_error {
background: #ddd;
font-size: 1em;
font-family:sans-serif;
text-align: left;
color: #333333;
}
#exception_error h1,
#exception_error h2 {
margin: 0;
padding: 1em;
font-size: 1em;
font-weight: normal;
background: #911911;
color: #FFFFFF;
}
#exception_error h1 a,
#exception_error h2 a {
color: #FFFFFF;
}
#exception_error h2 {
background: #666666;
}
#exception_error h3 {
margin: 0;
padding: 0.4em 0 0;
font-size: 1em;
font-weight: normal;
}
#exception_error p {
margin: 0;
padding: .4em;
}
#exception_error a {
color: #1b323b;
}
#exception_error p {
margin: 0;
padding: 0.1em 0;
}
#exception_error pre.source {
margin: 0 0 1em;
padding: 0.4em;
background: #fff;
border: dotted 1px #b7c680;
line-height: 1.2em;
}
#exception_error pre.source span.line {
display: block;
}
#exception_error pre.source span.highlight {
background: #f0eb96;
}
#exception_error pre.source span.line span.number {
color: #666;
}
</style>
<style type="text/css">
.linenum{
text-align:right;
background:#FDECE1;
border:1px solid #cc6666;
padding:0px 1px 0px 1px;
font-family:Courier New, Courier;
float:left;
width:17px;
margin:3px 0px 30px 0px;
}
code {/* safari/konq hack */
font-family:Courier New, Courier;
}
.linetext{
width:700px;
text-align:left;
background:white;
border:1px solid #cc6666;
border-left:0px;
padding:0px 1px 0px 8px;
font-family:Courier New, Courier;
float:left;
margin:3px 0px 30px 0px;
}
br.clear {
clear:both;
}
</style>
謝謝您的幫助,我知道這些都是一些非常牛逼這樣的問題,但我確實需要一些幫助,我不想拿起任何壞習慣。
關於實際「使用」類屬性的很好解釋 – Phil 2011-03-24 02:02:27
感謝您的幫助,我的意思是將它們設置爲public $ file;並用$ this-> file調用;所以問題是更多的,我應該爲每個屬性做,而且你對我的回答非常好,如果我認爲每個屬性都屬於大類,那將是瘋狂的。再次感謝+1 – JasonDavis 2011-03-24 02:16:25