/* * * @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
?
好習(xí)慣: 注釋函數(shù)和類例6里的注釋標(biāo)明了類和函數(shù)的意圖。注釋表明方法做了什么和為什么做,這會(huì)對(duì)將來(lái)了解代碼的意圖很有幫助。環(huán)境的變化會(huì)需要你進(jìn)行代碼修改,這就會(huì)讓很容易的知道開(kāi)始時(shí)你代碼是做什么的。例6.好習(xí)慣:注釋函數(shù)和類 ?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 }
?
通過(guò)檢驗(yàn)參數(shù)的全法性 這有助于他人使用你需要正確參數(shù)的函數(shù) 你應(yīng)該檢驗(yàn)它們并拋出異常的大意: 盡量拋出接近錯(cuò)誤的異常. 處理每個(gè)特殊的異常. 永遠(yuǎn),永遠(yuǎn)不要復(fù)制粘貼把代碼復(fù)制到你的編輯里的能力是一把雙刃劍。一方面,它避免了你參照一些示例后重新再打一遍時(shí)出現(xiàn)的錯(cuò)誤;另一方面,它讓書(shū)寫(xiě)相似代碼太簡(jiǎn)單了。你要避免在你的程序應(yīng)用中復(fù)制粘貼代碼。當(dāng)你發(fā)現(xiàn)自己在這樣做時(shí),停下來(lái)并問(wèn)自己可不可以把復(fù)制的部分重復(fù)使用。把相同的代碼放在同一個(gè)地方可以讓你以后修改時(shí)更加的輕松,因?yàn)橐淖兌荚谝黄?。壞?xí)慣:相似的代碼塊例9表現(xiàn)了除了一些值所在位置之外很相近的幾個(gè)方法。有些工具可以檢驗(yàn)?zāi)愕拇a中復(fù)制粘貼的部分(去看看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 ?
好習(xí)慣:可復(fù)用的帶參函數(shù)例10展示了把要復(fù)制的代碼入到一個(gè)方法中的代碼修改。另一個(gè)修改的方法則把工作代理給了一個(gè)新的方法 。編寫(xiě)一個(gè)通用的方法要花一些時(shí)間來(lái)設(shè)計(jì),當(dāng)然這會(huì)讓你停下來(lái)思考,而不是用復(fù)制粘貼的組合快捷鍵。但是這樣做會(huì)在以后修改時(shí)省回第一次多花的時(shí)間。例10.好習(xí)慣 :可利用的帶參函數(shù) ?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! ));