使用phpdoc记录子类中的继承函数/变量

时间:2013-07-22 13:35:18

标签: php phpdoc phpdocumentor2

假设我的父类有一些变量,它有phpdoc:

class Parent
{
    /**
     * This variable stores some important value that
     * is going to be changed in child classes
     * @var integer
     */
    public $variable = 0;
}

现在我正在编写子类,它具有此变量覆盖:

class Child extends Parent
{
    public $variable = 10;
}

所以我的问题是:我应该在Child类中为$ variable写什么phpdoc,所以我不必复制和粘贴变量描述?同样的问题适用于方法。非常感谢帮助。

更新: 我在创建了phpdoc之后看到错误后问过这个问题,比如Child类中的“没有关于property $ variable的摘要”。如果我添加此摘要 - 错误消失,但无论如何,phpdoc都显示来自Parent类的描述,无论我在Child类中编写什么。我做错了什么?

2 个答案:

答案 0 :(得分:0)

这种行为对我来说听起来像个错误。子类属性上完全缺少docblock会导致父属性docblock被完全继承。请在github repo报告此问题。

答案 1 :(得分:0)

您可以在类成员上使用 @inheritdoc,例如常量、属性、方法……并且它们可以从接口、父类、特征等继承……

示例:

interface MyInterface {
  
  /**
   * My doc
   *
   * @param string $myParam
   *
   * @return string|null
   */
  public function myMethodToImplement( string $myParam ) : ?string;
  
}

abstract class MyParentClass {
  
  /**
   * My inherited doc
   *
   * @param string $myParam
   *
   * @return string|null
   */
  public function myInheritedMethod( string $myParam ) : ?string {}
  
}


class MyClass extends MyParentClass implements MyInterface {
  
  /**
   * @inheritdoc
   */
  public function myInheritedMethod ( string $myParam ) : ?string {
    return parent::myInheritedMethod( $myParam );
  }
  
  
  /**
   * @inheritDoc
   *
   * @param bool $addedParam An added param
   *
   * You can add tags and still inherit from parent doc
   *
   * @throws NotImplementedException
   */
  public function myMethodToImplement ( string $myParam, bool $addedParam = false) : ?string {
    throw new NotImplementedException( __METHOD__ );
  }
}

使用 PHPStorm 等 IDE,我也有自动完成功能和快速文档。如果我使用 PHPDocumentor 生成此代码的文档,它也能正常工作。