Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(); } 
like image 922
tzortzik Avatar asked Jan 02 '13 00:01

tzortzik


People also ask

Can local variables be annotated?

So you can't retrieve an annotation on a local variable via reflection. I think that this kind of annotation is only used for compiler warnings. Local variable annotations are not retained in class files (or at runtime) regardless of the retention policy set on the annotation type. See JLS 9.6.

What is annotations in PHP?

PHP annotations are basically metadata which can be included in the source code and also in between classes, functions, properties and methods. They are to be started with the prefix @ wherever they are declared and they indicate something specific.

Does PHP have annotation?

Annotations can be placed in classes, methods, properties and functions. PHP offers only a single form of such metadata - doc-comments. In userland, there exist some annotation reader libraries like Doctrine Annotations which is widely used for eg. to express object-relational mapping metadata.


1 Answers

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”?
like image 153
hakre Avatar answered Sep 26 '22 02:09

hakre