NetBeans + PHP: type hinting

Sometimes, especially when dealing with variations of factory pattern, single method (namely factory()) can return objects of different types, so NetBeans is unable to guess the exact type of returned instance and as such cannot auto-complete. Indeed, you can setup return type using phpDoc syntax:

/**
 * @return Some_Base_Abstract_Class_Name
 */
public function factory($adapter)

but that doesn’t work if returned objects are specifications of more general abstract class (exactly the case with factory).

As it turned out, you can easily resolve this issue – just document your variable with @var, before calling factory() method:

/**
 * @var Some_Specific_Class $foo
 */
$foo = Magic::factory('adapterName');
$foo-> // and NetBeans opens pop-up list with available attributes and methods

The good news, you can use this method in any scope – it just works :) I love NetBeans!!

UPD: Well, actually NetBeans seems to be picky of scope – as reported by others (and confirmed by myself).

UPD1: Actually NetBeans handles this quite well, just use vdoc (review this and this for details)

  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit
  • Technorati
  • email
  • Print
  • DZone
  • eKudos
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter
,
Trackback

8 comments untill now

  1. It is a smart editor and I used it a lot for Java work, but it is too heavyweight for my work on php anyway.

  2. Rapha?l Dehousse @ 2009-07-17 14:24

    Hello,

    Are you sure you did not configure sth else to have this ?

    Because, I just downloaded Netbeans 6.7 and tried what you’re saying but I did not succeed

    Thanks for help !

    Cheers,

    Rapha?l

  3. Well, I re-checked with clean NetBeans install – and you are right, method described above doesn’t work as expected. I should definitely have tweaked IDE (I am on NB 6.5 btw), so that it behaves differently. Just not sure what that might be. Would review my configuration in depth, and try to figure that out..

  4. I am a great NetBeans fan and the IDE have been working really well with DooPHP framework with all the autocomplete and code hinting.
    Kudos to Netbeans!

  5. Rapha?l Dehousse @ 2009-07-30 15:10

    Hello,

    Did you find how you did it ?

    Thanks !

    Cheers,

    Rapha?l

  6. It looks like if you don’t set @return on Magic::factory() it will work ok, but it’s not too elegant not to define phpdocs. It would be great if NetBeans always prefer what you define via @var inline comment.

  7. I’ve just played with it a bit and I’ve found a sort of workaround. You can create dummy class (similarly to the following post http://www.mybelovedphp.com/2009/01/27/netbeans-revisited-code-completion-for-code-igniter-ii/) with new definition for factory that will overwrite original one. You omit @return tag there, e.g.

    class Magic {
    /**
    * @param string $name
    */
    static public function factory($name){}
    }

    then when you want to use it write is like that:

    $foo = Magic::factory(‘name’);
    /* @var $foo Some_Specific_Class */
    $foo-> // and auto-copletion works

    Note! @var inline comment must be below assignment. It is pretty dirty, but it actually works. Checked in NB 6.7.

  8. [...] to a post by about NetBeans + PHP: type hinting, I discovered a really great feature in Eclipse and NetBeans : inline autocompletion / type [...]

Add your comment now