Saturday, 3 December 2011

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:
  1. /** 
  2.  * @return Some_Base_Abstract_Class_Name 
  3.  */  
  4. 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:
  1. /** 
  2.  * @var Some_Specific_Class $foo 
  3.  */  
  4. $foo = Magic::factory('adapterName');  
  5. $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

No comments:

Post a Comment