php / phpDoc - @return $this 类的实例?

发布于 2024-10-11 23:38:17 字数 1085 浏览 5 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抱猫软卧 2024-10-18 23:38:17

更新:

从 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(){}
}

上一个答案:

我们对记录迭代器的 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(){}
}

Update:

As of Netbeans 7.4, the IDE supports @return self, static, and 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(){}
}

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.

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(){}
}
自由如风 2024-10-18 23:38:17

当我遇到一些事情时,我想我会重新审视这个问题。

目前不支持“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.

孤城病女 2024-10-18 23:38:17

!解决了! - 升级到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 以大 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! :)

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.

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/

つ可否回来 2024-10-18 23:38:17

这里有 3 个解决方法:
(这些只是解决方法。不得设计和实现类来影响 IDE 的行为)

选项 1:
将 foo 类中的方法 someOtherMethod 设为抽象或空方法

class foo implements ifoo {
    protected $_value = null;

    /**
     * Set something
     *
     * @param string $value the value
     * @return ifoo
     */
    public function setSomething($value) {
        $this->_value = $value;
        return $this;
    }

    // abstract method or create empty method if you want the method to be
    // to be optional 
    abstract function someOtherMethod();
}

选项 2:

覆盖 bar 类中的方法 setSomething

class bar extends foo {
    /**
     *
     * @param <type> $value
     * @return bar
     */
    public function setSomething($value) {
        return parent::setSomething($value); 
    }

    public function someOtherMethod(){}
}

选项 3:
使用界面

interface ifoo {
    public function someOtherMethod(){}
}

class foo {
    protected $_value = null;

    /**
     * Set something
     *
     * @param string $value the value
     * @return ifoo
     */
    public function setSomething($value) {
        $this->_value = $value;
        return $this;
    }
}

class bar extends foo implements ifoo {

    public function someOtherMethod(){}
}

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

class foo implements ifoo {
    protected $_value = null;

    /**
     * Set something
     *
     * @param string $value the value
     * @return ifoo
     */
    public function setSomething($value) {
        $this->_value = $value;
        return $this;
    }

    // abstract method or create empty method if you want the method to be
    // to be optional 
    abstract function someOtherMethod();
}

Option 2:

Override the method setSomething in bar class

class bar extends foo {
    /**
     *
     * @param <type> $value
     * @return bar
     */
    public function setSomething($value) {
        return parent::setSomething($value); 
    }

    public function someOtherMethod(){}
}

Option 3:
Use interface

interface ifoo {
    public function someOtherMethod(){}
}

class foo {
    protected $_value = null;

    /**
     * Set something
     *
     * @param string $value the value
     * @return ifoo
     */
    public function setSomething($value) {
        $this->_value = $value;
        return $this;
    }
}

class bar extends foo implements ifoo {

    public function someOtherMethod(){}
}
不奢求什么 2024-10-18 23:38:17

phpDoc 语法允许通过用 < 分隔来定义多种类型@return 标记 的 code>| 字符。当您使用 class bar 扩展 class foo 时,您应该编写一个新的 phpDoc 标记,该标记具有适合其 @return 的类。

如果函数返回 foobar,那么您将使用 @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 the class foo with class bar you should write a new phpDoc tag that has the proper class for its @return.

If a function returns either foo or bar then you would use @return foo|bar.

However in your case just define @return bar for the overridden function.

Take care.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文