记录 PHP,如果我扩展一个类,我应该复制/粘贴吗?

发布于 2024-08-25 22:12:19 字数 184 浏览 2 评论 0原文

我有一个带有方法的 PHP 类。在基类中(它更像是原型,但我没有使用原型,因为我们必须向后兼容),我记录了方法的参数和描述。

现在我延长该课程。在这个新方法(实现)中,我应该重新记录参数和描述,应该将其留空,还是应该只留下适用于该特定实现的相关注释?

我的目标是拥有由 PhpDoc 生成的可读 API 文档,并遵循约定。

I have a PHP class with a method. In the base class (it's more like a prototype, but I'm not using prototypes because we have to be backwards compatible), I document the parameters and description of the method.

Now I extend that class. In this new method (the implementation) should I re-document the parameters and description, should I leave it blank, or should I only leave relevant notes that apply to that particular implementation?

My goal is to have readable API docs produced by PhpDoc, and to follow conventions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长伴 2024-09-01 22:12:19

PhpDocumentor 将向您显示正在记录的方法是否是父类中该方法的重新定义以及该方法是否在子类中被重写。因此,除了您放入方法文档块中的所有信息之外,您还将看到有一个与当前方法关联的父方法和/或子方法。这意味着在方法的文档块中说某些内容对您有利。

我倾向于做的是将关键通用语言向上浮动到父方法,但我仍然对当前子方法以及孙方法有话要说。无论子方法与父方法有何不同,和/或该子方法与来自同一父方法的其他子方法的不同之处,都是该子方法文档块所需的信息。

我永远不会将父母的某些东西复制/粘贴到孩子身上......相反,我将进一步澄清是什么让孩子成为他的父母和/或他的兄弟姐妹。另外,我尝试谈论父文档块内的子元素,因为典型的父子关系是作为一种抽象方式来消除需要了解子元素的具体情况的。

PhpDocumentor is going to show you if the method being documented is a redefinition of the method in a parent class as well as if the method gets overridden in a child class. So, in addition to all info that you put in the method's docblock, you will also see that there is a parent method and/or child method associated to the current method. This means it is to your benefit to say something in the method's docblock.

What I tend to do is to float the key generic language upwards toward the parent method, but I'll still have something to say about the current child's method as well as a grandchild's method. Whatever differentiates the child method from the parent method, and/or whatever differentiates this child method from other child methods that are its peers from the same parent, is the info needed by this child method docblock.

I'm never going to copy/paste something from a parent to a child... I'm instead going to further clarify what makes the child who he is with regard to his parent and/or his siblings. Also, I try not to say anything about the children inside the parent's docblock, since the typical parent-child relationship is done as a way to abstract away needing to know the specifics of the children.

沧桑㈠ 2024-09-01 22:12:19

查看 Zend Framework 中的几个示例,似乎注释大多是复制粘贴的——这有时会导致不同的注释。

我将采用的第一个示例是 Zend_Http_Client_Adapter_Interface::connect,它被声明为:

/**
 * Connect to the remote server
 *
 * @param string  $host
 * @param int     $port
 * @param boolean $secure
 */
public function connect($host, $port = 80, $secure = false);

并且,如果您看一下实现此接口的类,例如 Zend_Http_Client_Adapter_Curl,你会看到:

/**
 * Initialize curl
 *
 * @param  string  $host
 * @param  int     $port
 * @param  boolean $secure
 * @return void
 * @throws Zend_Http_Client_Adapter_Exception if unable to connect
 */
public function connect($host, $port = 80, $secure = false)

所以,复制粘贴参数;以及实施中的更多信息。

另一个例子是 Zend_Log_Writer_Abstract::_write

/**
 * Write a message to the log.
 *
 * @param  array  $event  log data event
 * @return void
 */
abstract protected function _write($event);

并且,在子类中,如 Zend_Log_Writer_Db

/**
 * Write a message to the log.
 *
 * @param  array  $event  event data
 * @return void
 */
protected function _write($event)

这里,再次复制粘贴;以及父类中的一个小修改,该修改尚未在子类中重新创建。

现在,我一般做什么?

  • 我通常认为开发人员写注释的频率不够高
  • 并且通常忘记更新它们
  • 所以,我试图让他们的生活更轻松,并且不要重复注释
  • 除非子类中的注释必须与父类中的不同。

Looking at a couple of examples in Zend Framework, it seems the comments are mostly copy-pasted -- and this sometimes leads to different comments.

The first example I'll take is Zend_Http_Client_Adapter_Interface::connect, which is declared as :

/**
 * Connect to the remote server
 *
 * @param string  $host
 * @param int     $port
 * @param boolean $secure
 */
public function connect($host, $port = 80, $secure = false);

And, if you take a look at a class that implements this interface, like Zend_Http_Client_Adapter_Curl, you'll see :

/**
 * Initialize curl
 *
 * @param  string  $host
 * @param  int     $port
 * @param  boolean $secure
 * @return void
 * @throws Zend_Http_Client_Adapter_Exception if unable to connect
 */
public function connect($host, $port = 80, $secure = false)

So, copy-paste of the parameters ; and more informations in the implementation.

Another example would be Zend_Log_Writer_Abstract::_write :

/**
 * Write a message to the log.
 *
 * @param  array  $event  log data event
 * @return void
 */
abstract protected function _write($event);

And, in a child class, like Zend_Log_Writer_Db :

/**
 * Write a message to the log.
 *
 * @param  array  $event  event data
 * @return void
 */
protected function _write($event)

Here, again, copy-paste ; and a small modification in the parent class, that has not been re-created in the child class.

Now, what do I generally do ?

  • I generally consider that developpers don't write comments often enough
  • And generally forget to update them
  • So, I try to make their life easier, and don't duplicate comments
  • Unless the comment in the child class has to be different from the one in the parent class.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文