php / phpDoc - @return $this 类的实例?
如何在 phpDoc 中将方法标记为“返回当前类的实例”?
在下面的示例中,我的 IDE (Netbeans) 将看到 setSomething 始终返回一个 foo
对象。
但如果我扩展对象,情况就不是这样了 - 它会返回 $this
,在第二个示例中它是一个 bar
对象,而不是 foo
目的。
class foo {
protected $_value = null;
/**
* Set something
*
* @param string $value the value
* @return foo
*/
public function setSomething($value) {
$this->_value = $value;
return $this;
}
}
$foo = new foo();
$out = $foo->setSomething();
很好 - setSomething
返回一个 foo
- 但在下面的示例中,它返回一个 bar
..:
class bar extends foo {
public function someOtherMethod(){}
}
$bar = new bar();
$out = $bar->setSomething();
$out->someOtherMethod(); // <-- Here, Netbeans will think $out
// is a foo, so doesn't see this other
// method in $out's code-completion
... 那就太好了对于我来说,为了解决这个问题,代码完成是一个巨大的速度提升。
有人有一个聪明的技巧,或者更好的,一个用 phpDoc 记录这个的正确方法吗?
How do I mark a method as "returns an instance of the current class" in my phpDoc?
In the following example my IDE (Netbeans) will see that setSomething always returns a foo
object.
But that's not true if I extent the object - it'll return $this
, which in the second example is a bar
object not a foo
object.
class foo {
protected $_value = null;
/**
* Set something
*
* @param string $value the value
* @return foo
*/
public function setSomething($value) {
$this->_value = $value;
return $this;
}
}
$foo = new foo();
$out = $foo->setSomething();
So fine - setSomething
returns a foo
- but in the following example, it returns a bar
..:
class bar extends foo {
public function someOtherMethod(){}
}
$bar = new bar();
$out = $bar->setSomething();
$out->someOtherMethod(); // <-- Here, Netbeans will think $out
// is a foo, so doesn't see this other
// method in $out's code-completion
... it'd be great to solve this as for me, code completion is a massive speed-boost.
Anyone got a clever trick, or even better, a proper way to document this with phpDoc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
更新:
从 Netbeans 7.4 开始,IDE 支持 @return self、static 和 this (http://wiki. netbeans.org/NewAndNoteworthyNB74#Editor_2)。
上一个答案:
我们对记录迭代器的
current()
方法也有类似的问题。由于迭代器针对许多不同的类进行了扩展,因此与它关联的@return $class
是没有意义的。我们之前使用过@satrun77的选项2,但我使用过@method
在 Netbeans 中取得了一些成功。Update:
As of Netbeans 7.4, the IDE supports @return self, static, and this (http://wiki.netbeans.org/NewAndNoteworthyNB74#Editor_2).
Previous Answer:
We have a similar issue with a record iterator's
current()
method. Since the iterator is extended for many different classes, it doesn't make sense to have a@return $class
associated with it. We've used @satrun77's Option 2 before, but I've used@method
with some success in Netbeans.当我遇到一些事情时,我想我会重新审视这个问题。
目前不支持“return $this”,但有一个 PhpDoc 请求在 v1.5 中添加这一点:
http://pear.php.net/bugs/bug.php?id=16223
Eclipse PDT 中也有一个请求:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=276082
两者都是相对较旧的请求。我不会对很快实现这一点感到太兴奋,但这里希望:)与此同时,似乎这个问题没有适当的解决方案。
Thought I'd revisit this Q as I came across a couple of things.
Currently "return $this" isn't supported, but there is a PhpDoc request to add exactly that in v1.5:
http://pear.php.net/bugs/bug.php?id=16223
There's also a request for it in Eclipse PDT:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=276082
Both are relatively old requests. I'm not going to get too excited about this being implemented any time soon, but here goes to hoping :) In the meantime, it seems there is no proper solution to this problem.
!解决了! - 升级到netbeans 9.0(截至2018年7月稳定?)
我已经追求这个一年多了,终于有了一个开源解决方案! :)
这也适用于特征。截至 2018 年 11 月 1 日,9.0 以大 zip 形式提供(没有适用于 Windows、Mac 的全新安装程序?),您将不得不搜索添加 php 插件等,但它确实有效!我花了大约一个小时才把它全部设置好。我还安装了旧的 8.x,并与新的 9.0 一起运行,没有任何问题......到目前为止(只是不要同时运行它们)。插件提示: https://www.reddit.com/r/PHP/评论/9gtaaw/how_to_run_netbeans_9_with_php_support/
!SOLVED! - upgrade to netbeans 9.0 (stable as of July 2018?)
I have been after this for over a year and finally have an open source solution! :)
This also works with traits. As of 11/1/2018 9.0 comes as a big zip (no clean installer for windows, mac?) and you will have to search for adding the php plugings etc BUT IT DOES WORK! Took me about an hour to get it all set. I also have my old 8.x installed and running along side the new 9.0 without issue...so far (just don't run them both at same time). Plugin tip: https://www.reddit.com/r/PHP/comments/9gtaaw/how_to_run_netbeans_9_with_php_support/
这里有 3 个解决方法:
(这些只是解决方法。不得设计和实现类来影响 IDE 的行为)
选项 1:
将 foo 类中的方法 someOtherMethod 设为抽象或空方法
选项 2:
覆盖 bar 类中的方法 setSomething
选项 3:
使用界面
Here is 3 work around:
(These are just work around. classes must not be designed and implemented to sue the behavior of an IDE)
Option 1:
make the method someOtherMethod abstract or empty method in foo class
Option 2:
Override the method setSomething in bar class
Option 3:
Use interface
phpDoc 语法允许通过用 < 分隔来定义多种类型
@return
标记 的 code>| 字符。当您使用class bar
扩展class foo
时,您应该编写一个新的 phpDoc 标记,该标记具有适合其@return
的类。如果函数返回
foo
或bar
,那么您将使用@return foo|bar
。但是,在您的情况下,只需为覆盖的函数定义
@return bar
即可。小心。
phpDoc syntax allows for multiple types to be defined by separating them with a
|
character for the@return
tag. When you extend theclass foo
withclass bar
you should write a new phpDoc tag that has the proper class for its@return
.If a function returns either
foo
orbar
then you would use@return foo|bar
.However in your case just define
@return bar
for the overridden function.Take care.