annotating a local variable in php

孤者浪人 提交于 2021-02-05 14:50:08

问题


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

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

How can I do something like this?

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

回答1:


The Phpdoc standard does not cover these annotations (it only cover class properties with the @var tag); however, it is perfectly possible in Eclipse (e.g. 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.

Example Code:

<?php

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

Example Screenshot (Eclipse PDT (Indigo)):

Eclipse PDT (Indigo)

Related Question & Answers:

  • How do I make my PHP IDE understand Dependency Injection Containers?
  • Is there a way to make PhpStorm's autocomplete “go deeper”?



回答2:


This is an old question, but only for reference. You must include the Use statement for the Type in current file in order to @var annotation work

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

...

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


来源:https://stackoverflow.com/questions/14115654/annotating-a-local-variable-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!