PHPDoc 和后期(静态或动态)绑定
大多数 PHP IDE 依赖 phpdoc 来获取有关表达式类型的提示。然而,我经常使用这种模式,这似乎没有被涵盖:
class Control {
private $label = '';
/** @return ??? */
public static function Make(){ return new static(); }
/** @return ??? */
public function WithLabel($value){ $this->label = $value; return $this; }
/** @return void */
public function Render(){ /* ... */ }
}
class Textbox extends Control {
private $text = '';
/** @return ??? */
public function WithText($text){ $this->width = $text; return $this; }
}
现在我可以使用这样的类:
Textbox::Make() // <-- late static binding, returns Textbox
->WithLabel('foo') // <-- late dynamic binding, returns Textbox
->WithText('bar') // <-- normal binding, returns Textbox
->Render();
有什么方法可以用某些东西替换 '???' 以便输入信息正确吗?
Most PHP IDEs rely on phpdoc to get hints about the type of an expression. Yet, I use frequently this pattern, which doesn't seem to be covered:
class Control {
private $label = '';
/** @return ??? */
public static function Make(){ return new static(); }
/** @return ??? */
public function WithLabel($value){ $this->label = $value; return $this; }
/** @return void */
public function Render(){ /* ... */ }
}
class Textbox extends Control {
private $text = '';
/** @return ??? */
public function WithText($text){ $this->width = $text; return $this; }
}
Now I can use the classes like this:
Textbox::Make() // <-- late static binding, returns Textbox
->WithLabel('foo') // <-- late dynamic binding, returns Textbox
->WithText('bar') // <-- normal binding, returns Textbox
->Render();
Is there any way to replace the '???'s with something so that the typing information is correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于可以扩展的类中的静态方法:
对于最终静态方法:
对于非静态方法:
但在 phpdoc 手册中没有记录
For static methods in classes that may be extended:
For final-static methods:
For non-static methods:
but it's not documented in phpdoc manual
更新的答案作为参考最流行的 PHP IDE (PHPStorm 8):
对于
@return
,您可以使用:self
$this
对于
@method
> 你可以使用:$this
示例:
PHPStorm 10 (EAP) 更新
看来现在
static
也可以使用,但仅限于@return.
Updated answer taking as reference the most popular PHP IDE (PHPStorm 8):
For
@return
you can use:self
$this
For
@method
you can use:$this
Example:
Update for PHPStorm 10 (EAP)
It seems that now
static
can be used too, but only for@return
.更新了 cvsguimaraes 的答案以包含静态选项:
Updated cvsguimaraes' answer to include static options: