If you use Zend Studio as your IDE, here is a tip I have found useful. Zend Studio is pretty good at auto completion, but there are times when the property reference is too ambiguous. You can help the IDE with a few simple comments in the right places:

Example 1.0 – Comment tag above class property declaration

This one may common place to you if you are already documenting your code. By providing the declaration, Zend Studio can provide the API to the component.
Example 1.1 – Inline comment tag above variable declaration

This one is slightly different. It must be inline and it also declares the variable unlike the class property declaration. I also showed in this example that as soon as the variable is declared, the properties are available, even before a value is assigned. Of course, you wouldn’t use it like this but it shows that it is working.
Real World Examples
Example 2.1 – Looping through data
When analyzing data, I will often put the tag above the loop in order to get the auto completion for the item.
/* @var $user Person */ foreach ( $results as $user ) { echo $user->(AUTO COMPLETION HERE) } |
Example 2.2 – Ambiguous class references
Many times classes that are auto loaded have no problem executing when used, but the IDE does not know where to find these classes during authoring. I often find this is the case when I have libraries I have built that provide helper classes. Since there is no reference to the class or class instance directly, I will provide an inline tag to help with the auto completion.
// _helper inflects to find the right method to call // but the IDE does not know what class instance is // called nor what the result will be. /* @var $result Auth_Result */ $result = $this->_helper->authorize(); $result->(AUTO COMPLETION HERE) |
Example 2.3 – Variable declaration
This is common practice for me to provide the @var tag that the property represents. I am defining the property but the actual pointer happens later in code. If I did not have this then I would not get the auto completion later when referencing the property.
class My_Helper_Authorize extends Zend_Controller_Action_Helper_Abstract { /** * @var My_Auth_Roles */ protected $roles; public function init() { $this->roles = My_Auth_Roles::getInstance(); } public function someOtherMethod() { $this->roles->(AUTO COMPLETION HERE) } } |
Example 2.4 – Factory Pattern
This last example can be used by both comment types. If you use the Factory pattern to retrieve class instances, typically you have an Interface or Abstract classes you can rely on for some data, but if you want to know the full API for a given instance the auto-completion come in handy.
/** * @var Employee */ public $user; function __construct() { $this->user = Person::factory('employee'); } |
Hope this helps in your development as it did mine!
That was a great tip. I read about it awhile ago, but couldn’t get it to work. Thanks for the tip.