在 php 中注释局部变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14115654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:40:30  来源:igfitidea点击:

annotating a local variable in php

phpvariableslocalphpdoc

提问by tzortzik

I am using Eclipse PDT and I want to annotate a local variable using Phpdoc.

我正在使用 Eclipse PDT,我想使用 Phpdoc 注释局部变量。

All I see is that I can annotate the variables/properties of a class using @varor even @property, but how is this possible for a local variable?

我所看到的只是我可以使用@var甚至 注释类的变量/属性@property,但是这对于局部变量怎么可能呢?

How can I do something like this?

我怎么能做这样的事情?

function foo(){
  /** @var Stock $a */
  $a->save();
}

回答by hakre

The Phpdoc standard does not cover these annotations (it only cover class properties with the @vartag); however, it is perfectly possible in Eclipse (e.g. PDT):

Phpdoc 标准没有涵盖这些注解(它只涵盖带有@var标签的类属性);然而,这在 Eclipse 中是完全可能的(例如 PDT):

/* @var $variable Type */
 ^         ^        `--- type
 |      variable           
 |
 `--- single star

This also works in all other PHP IDEs like Netbeans or Phpstorm which is useful if you exchange your code with others.

这也适用于所有其他 PHP IDE,如 Netbeans 或 Phpstorm,如果您与其他人交换代码,这将非常有用。

Example Code:

示例代码:

<?php

/* @var $doc DOMDocument */
$doc->
?

Example Screenshot (Eclipse PDT (Indigo)):

示例截图(Eclipse PDT (Indigo)):

Eclipse PDT (Indigo)

Eclipse PDT(靛蓝)

Related Question & Answers:

相关问答:

回答by Gabriel

This is an old question, but only for reference. You must include the Usestatement for the Typein current file in order to @varannotation work

这是一个老问题,但仅供参考。您必须包含当前文件中的Use语句Type才能进行@var注释工作

<?php
use YourVendor\YourBundle\Entity\ProductType;

...

/* @var $product_type ProductType */
$foo = $product_type->getName();