led*_*neb 13 php netbeans phpdoc
如何在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();
Run Code Online (Sandbox Code Playgroud)
很好 - 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
Run Code Online (Sandbox Code Playgroud)
...解决这个问题对我来说很棒,代码完成是一个巨大的速度提升.
任何人都有一个聪明的技巧,甚至更好,一个正确的方法用phpDoc记录这个?
Pau*_*lRe 10
更新:
从Netbeans 7.4开始,IDE支持@return self,static和this(http://wiki.netbeans.org/NewAndNoteworthyNB74#Editor_2).
class foo {
protected $_value = null;
/**
* Set something
*
* @param string $value the value
* @return this
*/
public function setSomething($value) {
$this->_value = $value;
return $this;
}
}
class bar extends foo {
public function someOtherMethod(){}
}
Run Code Online (Sandbox Code Playgroud)
上一个答案:
我们在记录迭代器的current()
方法上遇到了类似的问题.由于迭代器是针对许多不同的类进行扩展的,因此@return $class
与它相关联是没有意义的.我们之前使用过@ satrun77的选项2,但我@method
在Netbeans中已经取得了一些成功.
class foo {
protected $_value = null;
/**
* Set something
*
* @param string $value the value
* @return foo
*/
public function setSomething($value) {
$this->_value = $value;
return $this;
}
}
/**
* @method bar setSomething($value)
*/
class bar extends foo {
public function someOtherMethod(){}
}
Run Code Online (Sandbox Code Playgroud)
我以为我遇到了很多事情,所以我会重温这个问题.
目前不支持"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
两者都是相对较旧的请求.我不会太兴奋,很快就会实施这个,但是这里有希望:)在此期间,似乎没有适当的解决方案来解决这个问题.
归档时间: |
|
查看次数: |
8014 次 |
最近记录: |