技术开发 频道

PHP 编程的 5 个良好习惯

  切忌使用复制粘贴

  您可以从其他地方将代码复制粘贴到自己的代码编辑器,但这样做有利也有弊。好的一面是,从一个示例或模板中复制代码能够避免很多错误。不好的一面是,这容易带来大量的类似编程方式。

  一定要注意,不要将代码从应用程序的一部分复制粘贴到另一部分。如果您采用这种方式,请停止这个不良的习惯,然后考虑将这段代码重写为可重用的。一般而言,将代码放置到一个地方便于日后的维护,因为这样只需在一个地方更改代码。

  不良习惯:类似的代码段

  清单 9 给出了几个几乎一样的方法,只是其中的值不同而已。有一些工具可以帮助找到复制粘贴过来的代码(参见 参考资料)。

  清单 9. 不良习惯:类似的代码段

<?php
/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Error"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countErrors($messages)
{
    $matchingCount
= 0;
    foreach($messages
as $m) {
        
if ($m->getSeverity() == "Error") {
            $matchingCount
++;
        }
    }
    return $matchingCount;
}

/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Warning"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countWarnings($messages)
{
    $matchingCount
= 0;
    foreach($messages
as $m) {
        
if ($m->getSeverity() == "Warning") {
            $matchingCount
++;
        }
    }
    return $matchingCount;
}

/**
* Counts the number of messages found in the array of
* ResultMessage with the getSeverity() value of "Information"
*
* @param $messages An array of ResultMessage
* @return unknown_type
*/
function countInformation($messages)
{
    $matchingCount
= 0;
    foreach($messages
as $m) {
        
if ($m->getSeverity() == "Information") {
            $matchingCount
++;
        }
    }
    return $matchingCount;
}

$messages
= array(new ResultMessage("Error", "This is an error!"),
    
new ResultMessage("Warning", "This is a warning!"),
    
new ResultMessage("Error", "This is another error!"));
    
$errs
= countErrors($messages);

echo(
"There are " . $errs . " errors in the result.\n");
?
>

  良好习惯:带参数的可重用函数

  清单 10 展示了修改后的代码,它将复制的代码放到一个方法中。另一个方法也进行了更改,它现在将任务委托给新的方法。构建通用的方法需要花时间设计,并且这样做使您能停下来思考,而不是本能地使用复制粘贴。但有必要进行更改时,对通用的方法投入的时间将得到回报。

  清单 10. 良好习惯:带参数的可重用函数

<?php
    
/*
    
* Counts the messages with the given severity in the array
    
* of messages.
    
*
    
* @param $messages An array of ResultMessage
    
* @return int Count of messages matching $withSeverity
    
*/
    
function countMessages($messages, $withSeverity)
    {
        $matchingCount
= 0;
        foreach($messages
as $m) {
            
if ($m->getSeverity() == $withSeverity) {
                $matchingCount
++;
            }
        }
        return $matchingCount;
    }

    
/**
    
* Counts the number of messages found in the array of
    
* ResultMessage with the getSeverity() value of "Error"
    
*
    
* @param $messages An array of ResultMessage
    
* @return unknown_type
    
*/
    
function countErrors($messages)
    {
        return countMessages($messages,
"Errors");
    }

    
/**
    
* Counts the number of messages found in the array of
    
* ResultMessage with the getSeverity() value of "Warning"
    
*
    
* @param $messages An array of ResultMessage
    
* @return unknown_type
    
*/
    
function countWarnings($messages)
    {
        return countMessages($messages,
"Warning");
    }

    
/**
    
* Counts the number of messages found in the array of
    
* ResultMessage with the getSeverity() value of "Warning"
    
*
    
* @param $messages An array of ResultMessage
    
* @return unknown_type
    
*/
    
function countInformation($messages)
    {
        return countMessages($messages,
"Information");
    }

    $messages
= array(new ResultMessage("Error", "This is an error!"),
        
new ResultMessage("Warning", "This is a warning!"),
        
new ResultMessage("Error", "This is another error!"));
        
    $errs
= countErrors($messages);

    echo(
"There are " . $errs . " errors in the result.\n");

?
>
0
相关文章