/* * * @param $dayOfWeek * @return int Day of week, with 1 being Monday and so on. */ function findNextBusinessDay($dayOfWeek) { $nextBusinessDay = $dayOfWeek;
switch($dayOfWeek) { case FRIDAY: case SATURDAY: case SUNDAY: $nextBusinessDay = MONDAY; break; default: $nextBusinessDay += 1; break; }
public function __construct($sev, $msg) { $this- severity = $sev; $this- message = $msg; }
public function getSeverity() { return $this- severity; }
public function setSeverity($severity) { $this- severity = $severity; }
public function getMessage() { return $this- message; }
public function setMessage($msg) { $this- message = $msg; } }
function cntMsgs($messages) { $n = 0; /* iterate through the messages... */ foreach($messages as $m) { if ($m- getSeverity() == 'Error') { $n++; // add one to the result; } } return $n; }
$messages = array(new ResultMessage( Error , This is an error! ), new ResultMessage( Warning , This is a warning! ), new ResultMessage( Error , This is another error! ));
$errs = cntMsgs($messages);
echo( There are . $errs . errors in the result./n
?
好习惯: 注释函数和类例6里的注释标明了类和函数的意图。注释表明方法做了什么和为什么做,这会对将来了解代码的意图很有帮助。环境的变化会需要你进行代码修改,这就会让很容易的知道开始时你代码是做什么的。例6.好习惯:注释函数和类 ?php /** * The ResultMessage class holds a message that can be returned * as a result of a process. The message has a severity and * message. * * @author nagood * */ class ResultMessage { private $severity; private $message;
/** * Constructor for the ResultMessage that allows you to assign * severity and message. * @param $sev See {@link getSeverity()} * @param $msg * @return unknown_type */ public function __construct($sev, $msg) { $this- severity = $sev; $this- message = $msg; }
/** * Returns the severity of the message. Should be one * Information , Warning , or Error . * @return string Message severity */ public function getSeverity() { return $this- severity; }
/** * Sets the severity of the message * @param $severity * @return void */ public function setSeverity($severity) { $this- severity = $severity; }
public function getMessage() { return $this- message; }
public function setMessage($msg) { $this- message = $msg; } }
/* * Counts the messages with the given severity in the array * of messages. * * @param $messages An array of ResultMessage * @return int Count of messages with a severity of Error */ function countErrors($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m- getSeverity() == Error ) { $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! ));
// Get the actual name of the function convertDayOfWeekToName($day) { $dayNames = array( Sunday , Monday , Tuesday , Wednesday , Thursday , Friday , Saturday return $dayNames[$day]; }
echo( The name of the 0 day is: . convertDayOfWeekToName(0) . /n echo( The name of the 10 day is: . convertDayOfWeekToName(10) . /n echo( The name of the 'orange' day is: . convertDayOfWeekToName('orange') . /n
/** * This is the exception thrown if the day of the week is invalid. * @author nagood * */ class InvalidDayOfWeekException extends Exception { }
class InvalidDayFormatException extends Exception { }
/** * Gets the name of the day given the day in the week. Will * return an error if the value supplied is out of range. * * @param $day * @return unknown_type */ function convertDayOfWeekToName($day) { if (! is_numeric($day)) { throw new InvalidDayFormatException('The value /'' . $day . '/' is an ' . 'invalid format for a day of week.'); }
if (($day 6) || ($day 0)) { throw new InvalidDayOfWeekException('The day number /'' . $day . '/' is an ' . 'invalid day of the week. Expecting 0-6.'); }
echo( The name of the 0 day is: . convertDayOfWeekToName(0) . /n
try { echo( The name of the 10 day is: . convertDayOfWeekToName(10) . /n } catch (InvalidDayOfWeekException $e) { echo ( Encountered error while trying to convert value: . $e- getMessage() . /n }
try { echo( The name of the 'orange' day is: . convertDayOfWeekToName('orange') . /n } catch (InvalidDayFormatException $e) { echo ( Encountered error while trying to convert value: . $e- getMessage() . /n }
?
通过检验参数的全法性 这有助于他人使用你需要正确参数的函数 你应该检验它们并抛出异常的大意: 尽量抛出接近错误的异常. 处理每个特殊的异常. 永远,永远不要复制粘贴把代码复制到你的编辑里的能力是一把双刃剑。一方面,它避免了你参照一些示例后重新再打一遍时出现的错误;另一方面,它让书写相似代码太简单了。你要避免在你的程序应用中复制粘贴代码。当你发现自己在这样做时,停下来并问自己可不可以把复制的部分重复使用。把相同的代码放在同一个地方可以让你以后修改时更加的轻松,因为要改变都在一起。坏习惯:相似的代码块例9表现了除了一些值所在位置之外很相近的几个方法。有些工具可以检验你的代码中复制粘贴的部分(去看看Resources)。例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! ));