如何对$ this变量使用docblock提示和类?

Nic*_*ick 1 php netbeans docblocks code-hinting

我使用NetBeans作为我的IDE.每当我有一些代码使用另一个函数(通常是工厂)来返回一个对象时,通常我可以执行以下操作来帮助提示:

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

但是,当我使用对象的属性来存储该类时,我有点不知道如何做同样的事情,因为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
    }
}
Run Code Online (Sandbox Code Playgroud)

我已尝试过该类的docblock,或使用上面的内联注释protected $foo或将foo设置为实例.

到目前为止我找到的唯一解决方法是:

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

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

    $extraVariable-> // now has hinting.
}
Run Code Online (Sandbox Code Playgroud)

我真的希望暗示在类范围内,因为许多其他函数可能会使用$this->foo,并且知道类的方法和属性将是有用的.

当然有一种更直接的方式......

Dan*_*oap 5

我不能说它在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
    }
}
Run Code Online (Sandbox Code Playgroud)