php - 是否应该在PHP中使用$ this-> var声明和访问方法属性?

标签 php debugging error-handling

我正在研究一些PHP类,这些类可以执行一些基本的性能分析,调试和错误处理。是的,我了解XDebug,它很棒,但是我也想为我的库提供一些类来做这类事情。

下面是我正在研究的一个类,当一个类发生时,它将显示一条错误消息,并向我显示其发生的行号,该行的源代码以及错误行上下的X行。类似于下面的这张图片...(可能需要在新窗口/选项卡中查看图片才能看到较大的版本)

这是我目前拥有的类(class)代码...

<?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)我应该在此类的顶部声明所有变量吗?例如,在此类中,我访问诸如 $ file $ 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> 

谢谢您的帮助,我意识到这些都是新问题,但我确实需要一些帮助,我不想养成任何不良习惯。

最佳答案

首先:声明类属性的方式是这样的:

class Debugger {
    public $file;
    private $line_number;
    private $padding;
}

现在,类属性仅在它们在相同对象上执行的操作(函数调用)之间保持不变时才有意义-换句话说,如果它们的值可以在调用之间“重用”。在您的情况下,其中有意义的唯一重用方法就是$padding(设置一次,然后使用相同的填充多次调用debug_source)。另外两个应保留功能参数。

第二:

绝对没有办法使Debugger成为自包含容器,并使其在页面上下文中同时生成有效的HTML,除非您使所有这些样式都内联。当然,这种解决方案远非最佳方案,但在这种情况下,它是唯一可以使用的解决方案。

关于php - 是否应该在PHP中使用$ this-> var声明和访问方法属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5413858/

相关文章:

c++ - Linux : Qt Creator debugger hangs while QQmlApplicationEngine is created

javascript - Mojarra:提供非缩小的 Javascript 文件

php - 获取查询时出现问题

php - mysql选择日期之间

java - Apache HttpClient 未显示响应的 Content-Length 和 Content-Encoding header

excel - Excel复制粘贴区域不同

PHP - 应该使用 set_error_handler 吗?

error-handling - 启动预先存在的重启的最快、最简单的方法是什么?

php - 我需要修复 mysql 查询以在 HTML 页面中显示一些内容

php - 解析 XML 并回显结果