Php DocBlock类类型继承
虽然这个问题是关于一般的,但我的用例是关于PHP的 考虑以下PHP代码:Php DocBlock类类型继承,php,javadoc,phpdoc,docblocks,Php,Javadoc,Phpdoc,Docblocks,虽然这个问题是关于一般的,但我的用例是关于PHP的 考虑以下PHP代码: <?php class ParentClass { /** * Says 'hi' to the world. * @return ParentClass Returns itself for chaining. */ public function say_hi(){ echo 'hi'; return $this; } }
<?php
class ParentClass {
/**
* Says 'hi' to the world.
* @return ParentClass Returns itself for chaining.
*/
public function say_hi(){
echo 'hi';
return $this;
}
}
class ChildClass extends ParentClass {
/**
* Says 'bye' to the world.
* @return ChildClass Returns itself for chaining.
*/
public function say_bye(){
echo 'bye';
return $this;
}
}
$c = new ChildClass;
$c->say_hi()->say_b| <- type hinting won't suggest "say_bye" here
?>
您所描述的通常称为“流畅接口”,其中对象的所有方法都执行其工作并返回对象本身
我个人还没有看到任何PHPDoc指南最终确定如何做到这一点。因此,我不知道任何IDE已经提出了一种方法来实现其自动完成功能来处理用例
PHPDoc可能采用的一种方法是使用“@return$this”作为表示fluent方法的约定,因为它与代码语法本身相匹配,因此非常清楚。我怀疑在标准本身包含此用例之前,任何IDE都不会在中构建该功能
在短期内,我认为您多余的“ChildClass::say_hi(){parent::say_hit();}”可能会使IDE自动完成工作。同样,可能是因为让autocomplete也识别方法链接本身(例如$foo->bar()->baz()->roll()->tide();)可能不存在。您可以这样解决这个问题:
class ParentClass {
/**
* Says 'hi' to the world.
* @return static
*/
public function say_hi(){
echo 'hi';
return $this;
}
}
“@return static”语句完全可以满足您的需要,PhpStorm可以很好地使用它。这似乎可以很好地使用Netbeans,尽管@return$this
会很方便……事实证明,phpDocumentor 2.x确实识别了“@return self”和“@return$this”,以实现该方法是一个“流畅”的方法。在生成的文档中,它突出显示“Fluent Interface”方法,并调整显示在chlid类文档中继承父方法的返回类类型。一旦2.0.0 stable正式发布,我想IDE将实现对这种语法的识别。希望他们的自动完成逻辑能够采取“流畅”的识别并正确调整方法继承。