0

用 PHP 编写它的最佳方法是什么,所以我知道哪个条件失败并且易于维护?无需诉诸多个 if else 语句...

if ((!$titleBlockPresent || !$leadBlock || ($allDoubleBlockCount !=2 || $allDoubleBlockCount!=1) ||$countFirstSmallShowBlocks !=2 ||$countSecondSmallShowBlocks !=2 ) && !$contentNotAvailableMessage)
{
    $this->fail("Block missing in the horizontal list of blocks on the non live carousel");
}
4

3 回答 3

1

试试这个

$shouldFail = FALSE;
switch(TRUE){
 case !titleBlockPresent:
    echo "No title block present<br/>";
    $shouldFail = TRUE;
 case !$leadBlock:
    echo "No lead block<br/>";

  // the rest of the code
}
于 2012-07-19T11:54:38.803 回答
0

如果您将该检查移到该函数中,那么您和其他任何查看您的代码的人都会很清楚,并且非常易于维护,例如:

function tester($var1, $var2, $var3)
{
    if (!$var1) 
    {
        $this->fail("error1");
        return FALSE;
    }
    if (!$var2) 
    {
        $this->fail("error2");
        return FALSE;
    }
    if (!$var3) 
    {
        $this->fail("error3");
        return FALSE;
    }
    return TRUE;
}

您还可以为每个if需要进一步澄清的评论添加评论。

于 2012-07-19T11:50:25.390 回答
0

我只是想出了这个,但注意到它与 GeoPhoenix 的答案非常相似,相反,可能也值得一试:

$bFail = false;

if(!$bFail && $contentNotAvailableMessage) $bFail = true;
if(!$bFail && !$titleBlockPresent ) $bFail = true;
if(!$bFail && !$leadBlock ) $bFail = true;

if(!$bFail && $allDoubleBlockCount != 2) $bFail = true;
if(!$bFail && $allDoubleBlockCount != 1) $bFail = true;
if(!$bFail && $countFirstSmallShowBlocks != 2) $bFail = true;
if(!$bFail && $countSecondSmallShowBlocks != 2) $bFail = true;

if($bFail) $this->fail("Block missing in the horizontal list of blocks on the non live carousel");
于 2012-07-19T12:03:24.843 回答