【问题标题】:Should method properties be declared and accessed with $this->var in PHP?是否应该在 PHP 中使用 $this->var 声明和访问方法属性?
【发布时间】:2011-03-24 01:47:23
【问题描述】:

我正在研究一些 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) 我应该在顶部声明这样的类中的所有变量吗?例如,在这个类中,我访问像 $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> 

感谢您的帮助,我知道这些都是一些非常新的问题,但我确实需要一些帮助,我不想养成任何坏习惯。

【问题讨论】:

    标签: php debugging error-handling


    【解决方案1】:

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

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

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

    第二:

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

    【讨论】:

    • 关于实际“使用”类属性的很好解释
    • 感谢您的帮助,我打算将它们设置为 public $file;并使用 $this->file 调用;所以问题更多了,我是否应该为每个属性都这样做,而你回答说对我来说很好,我想在一个大类中声明每个属性会有点疯狂。再次感谢 +1
    【解决方案2】:

    1) 否。属性声明不包含$this 关键字。您仅在访问属性时使用它,例如

    class Foo
    {
        private $foo;
        private $bar;
    
        public function __construct($foo, $bar)
        {
            $this->foo = $foo;
            $this->bar = $bar;
        }
    }
    

    2) 在这种情况下,我更倾向于在生成的标记中包含所有相关的style 属性。然后它将是真正独立的,并且是 XDebug 格式化其调试消息的方式。

    【讨论】:

    • 感谢您的信息,对不起,我不是故意要这样声明属性的,没有想清楚。所以我的主要问题是我应该声明所有使用的属性还是只声明其中的一些属性。乔恩回答了那部分。感谢您的帮助
    猜你喜欢
    • 2013-11-25
    • 2011-10-24
    • 2012-04-04
    • 2014-12-05
    • 1970-01-01
    • 2015-02-24
    • 2017-03-23
    • 1970-01-01
    • 2010-09-14
    相关资源
    最近更新 更多