简体   繁体   English

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

[英]Documenting PHP, should I copy/paste if I extend a class?

I have a PHP class with a method. 我有一个带有方法的PHP类。 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. 我的目标是拥有由PhpDoc生成的可读API文档,并遵循惯例。

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. PhpDocumentor将向您展示所记录的方法是否是父类中方法的重新定义,以及该方法是否在子类中被重写。 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. 因此,除了放在方法的docblock中的所有信息之外,您还将看到有一个与当前方法关联的父方法和/或子方法。 This means it is to your benefit to say something in the method's docblock. 这意味着在方法的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. 无论将子方法与父方法区分开来,和/或将此子方法与来自同一父方法的其他子方法区别开来的是该子方法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. 此外,我尽量不对父母的docblock中的孩子说些什么,因为典型的父子关系是为了抽象出需要了解孩子的具体情况。

Looking at a couple of examples in Zend Framework, it seems the comments are mostly copy-pasted -- and this sometimes leads to different comments. 看一下Zend Framework中的几个例子,似乎评论大多是复制粘贴的 - 这有时会导致不同的评论。

The first example I'll take is Zend_Http_Client_Adapter_Interface::connect , which is declared as : 我将采用的第一个例子是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);

And, if you take a look at a class that implements this interface, like Zend_Http_Client_Adapter_Curl , you'll see : 而且,如果你看一下实现这个接口的类,比如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)

So, copy-paste of the parameters ; 所以,复制粘贴的参数; and more informations in the implementation. 以及实施中的更多信息。


Another example would be Zend_Log_Writer_Abstract::_write : 另一个例子是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 : 并且,在子类中,如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. 除非子类中的注释必须与父类中的注释不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM