实例属性的显式类型



为了帮助编辑器更好地理解我的代码,我有时需要添加这样的注释:

/* @var $container Container */

这个工作很好,但有时我需要这样的东西:

/* @var $this->container Container */

有这样的东西吗?

先说几件事:

1。PHPDoc注释以/**开头

出于兼容性的考虑,PhpStorm也能理解普通/*注释中的PHPDoc标签,但你最好使用正确的符号。

2。@var标签的正确元素顺序如下:
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#517-var

/** @var [type] [element_name] [<optional description>] */

/** @var Container $container */

就像#1一样:PhpStorm理解这样的注释,即使元素被交换了(为了与其他(旧)ide/旧代码兼容)。


你的实际问题:

内联@var只允许局部/普通变量的类型提示。您不能将它用于复合变量(此处不能使用$this->container$someObject->someVar)。

这是错误的:

/* @var $this->container Container */
// even if it uses correct order/style
/** @var Container $this->container */

如果有的话:这样的类型提示应该在实际的类中提供,在实际的属性声明之上(省略[element_name]部分)https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#examples-15

class MyAwesomeClass
{
/** @var Container Optional description here */
protected $container;
...
}

最新更新