如何在phpDoc中将方法标记为“返回当前类的实例”?
在下面的示例中,我的集成开发环境(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();所以fine - 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记录这件事的适当方式?
发布于 2011-05-02 21:23:23
我想我应该重温一下这个问题,因为我遇到了一些事情。
目前不支持"return $this“,但是在v1.5中有一个PhpDoc请求来添加它:
http://pear.php.net/bugs/bug.php?id=16223
在Eclipse PDT中也有这样的请求:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=276082
这两个都是相对较旧的请求。我不会对这个即将实现的东西感到太兴奋,但这里是希望:)同时,似乎没有适当的解决方案来解决这个问题。
发布于 2011-05-02 21:41:50
更新:
从Netbeans 7.4开始,集成开发环境支持@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(){}
}上一个答案:
我们在记录迭代器的current()方法中也有类似的问题。由于迭代器针对许多不同的类进行了扩展,因此没有必要将@return $class与之关联。我们以前使用过@satrun77的选项2,但我在Netbeans中使用过@method,并取得了一些成功。
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(){}
}发布于 2018-11-02 07:08:13
!解决!-升级到netbeans 9.0 (2018年7月稳定?)
我已经追求了一年多了,终于有了一个开源的解决方案!:)
class Input extends BasicHtml
{
public function someOnlyInputFunc()
{
}
}
class Table extends BasicHtml
{
public function tableOnlyFunc()
{
}
}
abstract class BasicHtml
{
/**
*
* @param array $arrayForNow
* @return $this
*/
public function setStyle( array $arrayForNow )
{
return $this;
}
}
/////driver
$table = new Table();
$input = new Input();
$input->setStyle(array())->//now shows only Input + baseHtml functions
$table->setStyle(array())-> //now shows only Table + baseHtml functions
///note - in 8.0.2 version it shows blank obj drop downs on exact same code.这也适用于特征。截至2018年11月1日,9.0作为一个很大的压缩包(没有干净的windows安装程序,mac?)您将不得不搜索添加的php插件等,但它确实有效!我花了大约一个小时才把它弄好。我也安装了我的旧8.x,并与新的9.0一起运行,但没有使用issue...so far (只是不要同时运行它们)。插件提示:https://www.reddit.com/r/PHP/comments/9gtaaw/how_to_run_netbeans_9_with_php_support/
https://stackoverflow.com/questions/4705768
复制相似问题