Search code examples
phpvariableslocalphpdoc

annotating a local variable in php


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();
}

Solution

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