annotating a local variable in php

24,711

Solution 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:

Solution 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();
Share:
24,711
tzortzik
Author by

tzortzik

Updated on July 05, 2020

Comments

  • tzortzik
    tzortzik almost 4 years

    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();
    }
    
  • tzortzik
    tzortzik over 11 years
    I tried like this but it still fails: function foo(){ /* @var $a Stock */ $a->save(); }
  • KingCrunch
    KingCrunch over 11 years
    @tzortzik What do you mean with "it fails"? You know, that it is only for inspection?
  • hakre
    hakre over 11 years
    @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.
  • tzortzik
    tzortzik over 11 years
    @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.
  • hakre
    hakre over 11 years
    @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.
  • Tim
    Tim over 10 years
    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.
  • Олег Всильдеревьев
    Олег Всильдеревьев almost 10 years
    Nice, it works for me. Zend Eclipse for PHP Developers Version: 3.2.0
  • hakre
    hakre over 9 years
    @Pratsgogo: Thanks for your link, but if you read the answer I've already written that the phpdoc standard does not cover such variables.
  • Marcel Djaman
    Marcel Djaman over 8 years
    /* @var $product_type \YourVendor\YourBundle\Entity\ProductType */ works too
  • Gabriel
    Gabriel over 8 years
    @marcel-djaman: Yes, of course. My answer points to the fact that it is necessary indicate the fully qualified name of the Type.
  • marcovtwout
    marcovtwout almost 7 years
    If you want NetBeans to support alternative syntaxes as well, please comment/vote on the feature request: netbeans.org/bugzilla/show_bug.cgi?id=267470
  • Kamafeather
    Kamafeather over 6 years
    Note: as said PHPDoc doesn't support this annotation over non-property variables, but as convention it's better to stick to its DocBlock definition; hence the comment should start with double asterisk /**.
  • hakre
    hakre over 6 years
    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.