引用注释手册:
PHP 支持“C”、“C++”和 Unix shell 样式(Perl 样式)cmets。例如:
<?php
echo 'This is a test'; // This is a one-line c++ style comment
/* This is a multi line comment
yet another line of comment */
echo 'This is yet another test';
echo 'One Final Test'; # This is a one-line shell-style comment
?>
一般来说,你会想要avoid using comments in your sourcecode。引用 Martin Fowler 的话:
当你觉得需要写评论时,首先尝试重构代码,让任何评论都变得多余。
意思是这样的
// check if date is in Summer period
if ($date->after(DATE::SUMMER_START) && $date->before(DATE::SUMMER_END)) {
应该改写成
if ($date->isInSummerPeriod()) { …
您有时会遇到的另一种注释类型是分隔符注释,例如像
// --------------------------------------------
或
################################################
这些通常表明它们使用的代码做得太多。如果您在一个类中发现了这种情况,请检查该类的职责,看看它的某些部分是否可以更好地重构为一个独立的类。
对于 API 文档,常见的符号是 PHPDoc,例如
/**
* Short Desc
*
* Long Desc
*
* @param type $name description
* @return type description
*/
public function methodName($name) { …
如果剩余的方法签名清楚地传达了它的作用,我认为你可以省略 Short 和 Long Desc。但是,这需要一定的纪律和知识来实际编写Clean Code。例如,以下内容是完全多余的:
/**
* Get the timestamp property
*
* The method returns the {@link $timestamp} property as an integer.
*
* @return integer the timestamp
*/
public function getTimestamp() { …
并且应该缩短为
/**
* @return integer
*/
public function getTimestamp() { …
不用说,您是否选择完整的 API 文档也取决于项目。我希望任何我可以下载和使用的框架都有完整的 API 文档。重要的是,无论您决定做什么,都要始终如一地去做。