36

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();
}
hakre
  • 193,403
  • 52
  • 435
  • 836
tzortzik
  • 4,993
  • 9
  • 57
  • 88
  • 1
    Documentation/contracts generally only applies to exposed interfaces - e.g. fields and methods. –  Jan 02 '13 at 00:38

2 Answers2

73

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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I tried like this but it still fails: function foo(){ /* @var $a Stock */ $a->save(); } – tzortzik Jan 02 '13 at 00:57
  • @tzortzik What do you mean with "it fails"? You know, that it is only for inspection? – KingCrunch Jan 02 '13 at 01:02
  • @tzortzik: Which editor plugin are you using in Eclipse to edit PHP files? E.g. for me this perfectly works with code-completition in PDT (hammering CTRL + SPACE). Give Eclipse/DLTK some time to update, probably you need to place the comment in the line above the variable where you want to use it. At least that is how I normally do it. Not to the left of it but above. Like in your question. – hakre Jan 02 '13 at 01:05
  • @hakre: I did everything you said until my first post. I tried everything. I use the latest version of Eclipse PDT. I updated everything that is related to PHP and Eclipse. – tzortzik Jan 02 '13 at 01:12
  • @tzortzik: I can not explain why it does not work for you. I just create an example code and added a screenshot how it works for me in Eclipse Indigo PDT. – hakre Jan 02 '13 at 13:24
  • I find that if you obtain `$doc` from a function that has a `@return Type` annotation, this breaks the local `@var Type` inline annotation. Try commenting about the line that sets `$doc =` and you should get your hints. – Tim Jan 18 '14 at 18:10
  • Nice, it works for me. Zend Eclipse for PHP Developers Version: 3.2.0 – Олег Всильдеревьев Aug 06 '14 at 15:27
  • @Pratsgogo: Thanks for your link, but if you read the answer I've already written that the phpdoc standard *does not cover* such variables. – hakre Dec 05 '14 at 10:12
  • If you want NetBeans to support alternative syntaxes as well, please comment/vote on the feature request: https://netbeans.org/bugzilla/show_bug.cgi?id=267470 – marcovtwout May 23 '17 at 14:17
  • **Note:** as said PHPDoc doesn't support this annotation over non-property variables, but as convention it's better to stick to its [DocBlock](https://docs.phpdoc.org/guides/docblocks.html) definition; hence the comment should start with double asterisk ```/**```. – Kamafeather Jan 16 '18 at 11:53
  • I don't think there is any better. It's just undefined with an outcome of two variants and both have to be supported at that place. – hakre Jan 17 '18 at 08:23
6

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();
Gabriel
  • 161
  • 2
  • 3