X

An Oracle blog about NetBeans for PHP

Defining a variable type in comment

Yesterday was the first day at work this year and we committed new feature, which allow to define a type for a variable in a comment. The comment has to be defined in specific form as is displayed on the picture.

The comment has to be /\* @var $variable type \*/ . If the comment is written in the right form, then the var tag has bold font.

You can use this helper, when NetBeans is not able to recognize the type of the variable. On the picture below you can see that NetBeans recognizes that the variable $media in the echo statement is Book type. But then another object is assigned to the $media variable through the getLastMovie() function. This function defines the return type Movie, so from this line NetBeans knows that $media variable contains an object of Movie type. 

NetBeans handles the variable name and the type from the comment helper as are handled in PHP Doc. So for example mark occurrences works as usual.

Next time I will show you how NetBeans can help you with writing the comment helper.

Join the discussion

Comments ( 41 )
  • Stefan Sturm Tuesday, January 6, 2009

    Hello,

    great new feature :-)

    But I found a bug:

    When I use this Code everything is OK:

    /\* @var $l_value Image \*/

    foreach ( $imageList as $l_value )

    {

    $l_value->...

    }

    But with this Code, I get an Exception:

    foreach ( $imageList as $l_value ) /\* @var $l_value Image \*/

    {

    $l_value->...

    }

    Greetings,

    Stefan Sturm


  • Petr Tuesday, January 6, 2009

    To Stefan: thanks for finding this problem. I'm going to fix it today.


  • Sam Tuesday, January 6, 2009

    Good one. It was implemented first in Zend Studio 6 if I remember correctly. Helped me a lot when I was using ZSE.


  • Leszczu Tuesday, January 6, 2009

    Great feature, but how about arrays of object? How to make Netbeans to complete this?:

    $a = array();

    $a[] = new Movie();

    $a[] = new Movie();

    $a[0]-> [code competition here]

    foreach ($a as $element) {

    $element-> [code competition here]

    }


  • Petr Tuesday, January 6, 2009

    The problem, which was mentioned by Stefan is now fixed.


  • Jacob Tuesday, January 6, 2009

    Code completion doesn't seem to work in the following situation.

    $something->anotherthing()->|

    even tho I have PHPDoc'ed the function anotherthing to return a different object type, I still get the codecompletion of the something object.


  • Omar Wednesday, January 7, 2009

    This is the most important feature I was waiting for.

    Thanks a lot.


  • Petr Wednesday, January 7, 2009

    To Jacob: strange it works for me. At least what I tried:

    <?php

    class Issue {

    public function getDescription() {

    }

    }

    class Bug {

    function getDefaultPriority() {

    return 3;

    }

    /\*\*

    \* @return Bug

    \*/

    public static function getLastBug() {

    }

    /\*\*

    \* @return Issue

    \*/

    public function depends() {

    }

    }

    /\*\*

    \* @return Bug

    \*/

    function getFirstBug() {

    }

    /\* @var $something DOMAttr \*/

    $something->appendChild();

    $something = Bug::getLastBug();

    $something->depends()->getDescription();

    ?>

    Could you try this example? Let me know, whether it work for you. If yes, then we should investigate more, why it doesn't work in your case.

    Thanks,

    Petr


  • Amenthes Wednesday, January 7, 2009

    Regarding Leszczu's question.

    It would be nice to support something like:

    /\*\*

    \* @var array[Bar]

    \*/

    $foo = array(new Bar, new Bar);


  • Tomas Prochazka Wednesday, January 7, 2009

    This is the most important feature I was waiting for. Thanks a lot.

    Now is Netbeans practically better than commercial Zend Studio!!!

    I miss only Zend Toolbar feature. It's very practical for run debugger from any state of application. How I can in Netbeans log to web application, make same operation and then run debugger after form submit?


  • Keith Davis Thursday, January 8, 2009

    I'm not sure what I am doing wrong, but it is not working for me. I use lazy loading. Could that be it?


  • Petr Thursday, January 8, 2009

    Hi Keith,

    it's not connected with lazy loading. Which build do you use?


  • jsd Thursday, January 8, 2009

    Fantastic!! I have been waiting for this for so long.


  • planetthoughtful Thursday, January 8, 2009

    Just wondering how you get the build in which this has been enabled? I'm using Netbeans 6.5 on Windows Vista.


  • gawan Friday, January 9, 2009

    to planetthoughtful: download development version here: http://bits.netbeans.org/download/trunk/nightly/latest/


  • infozone Saturday, January 10, 2009

    Thanks for your effort - lack of this feature often makes me think of getting back to Eclipse.

    Doesn't work for me with member vars though:

    class a

    {

    function x() {}

    }

    class b

    {

    /\* @var member a; \*/

    var $var_of_type_a;

    function x()

    {

    $this->var_of_type_a-> // No suggestions

    }

    }


  • infozone Saturday, January 10, 2009

    Sorry for typo:

    /\* @var member a; \*/ should had been

    /\* @var var_of_type_a a \*/ of course.

    Doesn't work anyway. For what it worth, tested with 200901091401 nightly build.


  • Petr Saturday, January 10, 2009

    To infozone:

    There is PHP doc definition for classfield:

    http://manual.phpdoc.org/HTMLframesConverter/default/phpDocumentor/tutorial_tags.var.pkg.html

    As you can see, it's used the @var field, but the form is:

    /\*\* @var type \*/

    So for class fields use the PHP doc.

    Regards,

    Petr


  • infozone Sunday, January 11, 2009

    Oh, ok :) Thanks Petr, you're indeed correct. Then, wouldn't it be too difficult to make vdoc distinct whether it is used on a class member or a standalone var and apply corresponding syntax automatically?

    Or better yet, make auto documentor (or whatever you call it) work similarly? I mean, if you type:

    /\*\*<enter>

    above class var declaration, it results in

    /\*\*

    \* @var <type>

    \*/

    with cursor located on the right of <type> placeholder, so that you need to erase it manually to indicate a type - very inconvenient. Should it select the placeholder and allow to type in an appropriate type over it, it would be much better. Actually even just removal of the <type> placeholder would be good in this case - although this doesn't solve similar issue with methods params, where you are getting something like:

    /\*\*

    \*

    \* @param <type> $a

    \* @param <type> $b

    \*/

    with cursor located after $b, and then cannot even select <type> with just double-click, as it selects only word "type" but not the angle braces around it, so they require additional typing to be deleted.

    Thanks again for your effort.


  • gawan Monday, January 12, 2009

    Hi Petr, nice feature, but I found a bug. Try this example:

    <?

    /\*\*

    \* @property string $title

    \* @property string $body

    \*/

    class Blog {}

    /\*\*

    \* @property string $name of user

    \* @property Blog $blog

    \*/

    class User {}

    /\* @var $user User \*/

    $user->blog->

    ?>

    and after CTRL + SPACE netbeans offers me property of User not of Blog. I already added it into issuezilla: http://www.netbeans.org/issues/show_bug.cgi?id=156614

    Little enhancement: Could you add text after @property into PHPDoc popup window (e.g. for property $name -> "$name of user") ?


  • Petr Monday, January 12, 2009

    To infozone:

    I will think about your suggestion. You are right, that the current approach is not consistent.

    To gowan:

    Thanks for entering the issue. I have put a comment there.


  • Joao Neto Wednesday, February 25, 2009

    Great!

    The PHP plugin for netbeans improves every day helping our work!

    Thanks!


  • James Dempster Sunday, March 8, 2009

    This is a fantastic addition. I'm so close to leaving Eclipse PDT behind.

    I am having one problem though.

    See example

    abstract class SimpleObject {

    public function methodOne() {

    }

    public function methodTwo() {

    }

    }

    class ComplexObject extends SimpleObject {

    public function methodThree() {

    }

    }

    class Foo {

    /\*\*

    \* @var SimpleObject

    \*/

    private $_object;

    public function __construct(SimpleObject $example) {

    $this->_object = $example;

    }

    /\*\*

    \* Example method returns somthing like SimpleObject

    \*

    \* @return SimpleObject

    \*/

    public function getOject() {

    return $this->_object;

    }

    }

    $foo = new Foo();

    $fooObject = $foo->getOject();

    /\* @var $fooObject ComplexObject \*/

    $fooObject->

    Here I would want the vardoc to change what the editor thinks it has in $fooObject. Because I know what the object type is.


  • Henrik Monday, May 18, 2009

    Hi - I have a problem not far from the problem Jacob statet. I have problems "type-casting" $this - and return value of methods are not properly working (when using an object "type-cast" via comments)

    <?php

    class Issue {

    public function getDescription() {

    }

    /\*\*

    \*

    \* @return Bug

    \*/

    public function getLastBug() {

    }

    }

    class Bug {

    function getDefaultPriority() {

    return 3;

    }

    /\*\*

    \* @return Issue

    \*/

    public function depends() {

    }

    }

    /\* @var $this Issue \*/

    //$this resolves to "Bug" (Error)

    $this->depends();

    /\* @var $bug Bug \*/

    //$bug resolves to Bug - All ok...

    $bug->depends();

    /\* @var $issue Issue \*/

    //Return value of getLastBug resolves to Issue (Error)

    $issue->getLastBug()->getLastBug()->getLastBug();

    //Here everything is working:

    $issue2 = new Issue();

    $issue2->getLastBug()->depends();

    I use $this "type-casting" alot since i use a Zend Framework approach to MVC. so a fix for this would be great! (Netbeans reacts so much faster than Zend Studio - speeding up production a lot!)

    Im using the Netbeans 6.7 Beta (Just downloaded yesterday)


  • Ali Saturday, July 4, 2009

    Hi

    Really appreciate this feature but I think this feature is built keeping PHP as structured language.

    My code is

    function _initViewHelpers(){

    $view = $this->_layout->getView();

    }

    where getView is a ZendFramework and returns Zend_View_Interface, but IDE shows ? for object $view after that line of code.

    My work around which I hate to do is

    class Bootstrap extends Zend_Application_Bootstrap_Bootstrap

    {

    /\*\*

    \*

    \* @var Zend_View_Interface

    \*/

    protected $_view;

    function _initViewHelpers()

    {

    $this->_view = $this->_layout->getView();

    }

    }

    As this you can see i have to put $this on every line.


  • medyum Thursday, August 20, 2009

    Just wondering how you get the build in which this has been enabled? I'm using Netbeans 6.5 on Windows Vista.....

    Thanks for sharing


  • links of london Thursday, October 29, 2009

    It was a very nice idea! Just wanna say thank you for the information you have shared. Just continue writing this kind of post. I will be your loyal reader. Thanks again.


  • thinsoldier Monday, December 21, 2009

    Why doesn't it work with regular phpdoc syntax?

    /\*\*

    \* @var $usermodel ResidentsModel

    \*/


  • thinsoldier Monday, December 21, 2009

    how would I add class identification using normal phpdoc syntax?

    example from http://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.var.pkg.html

    class class1

    {

    /\*\*

    \* example of documenting a variable's type

    \* @var string

    \*/

    var $variable;

    /\*\*

    \* example of documenting a variable's type

    \* @var string contains class1 information

    \*/

    var $variable_with_desc;

    /\*\*

    \* this variable is documented as type "mixed" since no @var tag is present

    \*/

    var $mixed_variable;

    }


  • thinsoldier Monday, December 21, 2009

    Regarding my previous comment

    I have a few files where this works:

    /\*\*

    \* @var EmployeesModel $employeesModel

    \*/

    public $employeesModel;

    But it doesn't seem to start working anywhere close to immediately after typing it in a new file.

    Only seems to work in files after I've saved them, closed them, waited a few minutes, and reopened them.


  • thinsoldier Monday, December 21, 2009

    Single line format:

    /\* @var $VARIABLE CLASS \*/

    This works.

    Multi-line PHPDoc Style format:

    /\*\*

    \* @var CLASS $VARIABLE

    \*/

    This works

    But they're listing the items in completely different orders. Single line has class after variable. Multi line has class before variable.

    With the single line format, how can I also add a text description of the variable along with its class type all on one line?


  • thinsoldier Monday, December 21, 2009

    Something needs to be done about the spam in your comments


  • thinsoldier Friday, January 22, 2010

    I've got a big problem with that /\* @var $media Book \*/ Example.

    Some of my files only have working code completion if I write the comment like

    /\*\* @var Book $media \*/

    Others only work if i write the comment like

    /\* @var $media Book \*/


  • zurom Thursday, March 4, 2010

    "... the var tag has bold font..."

    -------

    In the Netbeans 6.8 bold Highlighting doesn't work anymore


  • alexander mcqueen heels Monday, August 16, 2010

    But it doesn't seem to start working anywhere close to immediately after typing it in a new file.


  • Emanuil Wednesday, March 9, 2011

    Thanks NetBeans PHP team for the great work you do! Can't wait for version 7 to come out.


  • Stefan Mikhail Saturday, October 22, 2011

    I'm new to PHP and therefore also NetBeans. Therefore please forgive my question if it is daft.

    Why would you want to place the variable type in a comment? Why does it matter? Is there any purpose besides having the information in the code?

    Just trying to understand the purpose of this feature a little better.

    Thanks


  • Petr Pisl Sunday, October 23, 2011

    @Stefan: In certain cases the IDE is not able to recognized the type of a variable. PHP is not strongly typed language as for example Java is. So sometimes you need to help to ide to provide right code completion and other features, that are typed based.


  • Stefan Mikhail Sunday, October 23, 2011

    @Petr - thanks for the information. So in my case I have a variable called $clause. I used gettype to find the type and it is a string, so how would I make use of this feature for my variable?


  • magnetik Wednesday, October 3, 2012

    It seems that it has been removed in Netbeans 7.2

    WHY ?!!


  • Ondrej Brejla Thursday, October 4, 2012

    It wasn't. It still works.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services