php/phpDoc-@返回此类的实例? [英] php / phpDoc - @return instance of $this class?
问题描述
如何在phpDoc中将方法标记为返回当前类的实例"?
How do I mark a method as "returns an instance of the current class" in my phpDoc?
在下面的示例中,我的IDE(Netbeans)将看到setSomething始终返回foo
对象.
In the following example my IDE (Netbeans) will see that setSomething always returns a foo
object.
但是如果我扩展对象,那是不对的-它会返回$this
,在第二个示例中,它是bar
对象而不是foo
对象.
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();
很好-setSomething
返回foo
-但在以下示例中,它返回bar
..:
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.
任何人都有一个巧妙的技巧,或者甚至更好的方法,可以用phpDoc记录下来吗?
Anyone got a clever trick, or even better, a proper way to document this with phpDoc?
推荐答案
当我遇到两件事时,我会重新考虑这个问题.
Thought I'd revisit this Q as I came across a couple of things.
当前不支持返回$ this",但是有一个PhpDoc请求将其完全添加到v1.5中:
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
Eclipse PDT中也有一个要求:
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.
这篇关于php/phpDoc-@返回此类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!