簡體   English   中英

如何為$ this變量的類使用docblock提示?

[英]How can I use docblock hints with classes for $this variables?

我使用NetBeans作為我的IDE。 每當我有一些使用另一個函數(通常是工廠)的代碼返回對象時,通常我可以執行以下操作來提示:

/* @var $object FooClass */
$object = $someFunction->get('BarContext.FooClass');
$object-> // now will produce property and function hints for FooClass.

但是,當我使用對象的屬性來存儲該類時,我有點不知所措,因為trying to use @var $this->foo or @var foo不會通過以下方式進行提示:

use Path\To\FooClass;

class Bar
{
    protected $foo;

    public function bat()
    {
        $this->foo = FactoryClass::get('Foo'); // Returns an instance of FooClass

        $this->foo //does not have hinting in IDE
    }
}

我已經在該類的docblock中嘗試過,或者在protected $foo或其中foo設置為實例的情況下使用了內聯注釋。

到目前為止,我發現的唯一解決方法是:

public function bat()
{
    $this->foo = FactoryClass::get('Foo');

    /* @var $extraVariable FooClass */
    $extraVariable = $this->foo;

    $extraVariable-> // now has hinting.
}

我真的很想讓提示在整個類范圍內,因為許多其他函數可能會使用$this->foo ,並且知道類的方法和屬性將很有用。

當然,有一種更直接的方法...

我不能說它如何在Netbeans中工作,但是在PHPEclipse中,您可以將提示添加到變量本身的聲明中:

use Path\To\FooClass;

class Bar
{
    /**
     * @var FooClass
     */
    protected $foo;

    public function bat()
    {
        $this->foo = FactoryClass::get('Foo'); // Returns an instance of FooClass

        $this->foo // should now have hinting
    }
}

給定

class Bar
{
    protected $foo;

    public function bat()
    {
        $this->foo = FactoryClass::get('Foo'); // Returns an instance of FooClass

        $this->foo //does not have hinting in IDE
    }
}

IDE試圖從FactoryClass::get ,該聲明可能沒有docblock返回類型。 問題是,如果此工廠方法可以返回任意數量的類,那么除了使用變通方法外,您無能為力。

否則,它將不會知道FactoryClass::get('Foo')FactoryClass::get('Bar')之間的區別,因為這兩個調用很可能會返回不同類型的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM