Code completion for a class field
By Petr on Oct 09, 2008
NetBeans are able to offer code completion for class fields of a PHP class. Because PHP is not a strict typed language, you can help to NetBeans to find out the type of a field.
Let's have a simple example:
The example doesn't show good practice, because the class fileds shouldn't be accessible outside the class, but for explanation it's enough.
One way how NetBeans are trying to find out the type of a field is that it looks through the class constructor. In our case in constructor there is expression that assigns new Address object to the $address field. So NetBeans supposes that the field is Address type and code completion for $this->address-> suggests $street and $city fields from Address object.
This functionality was added recently and works only in the file, where the class is defined and still there is a space for improvements.
The second way is to specify the type of a field through PHP Doc. It's more universal, the field doesn't have to be initialized in the constructur. As you can see on the picture, the type is also displayed in the code completion window. NetBeans helps to write the PHP Doc, write /\*\* one line above the field and press ENTER. Then NetBeans will generate appropriate PHP Doc. Also you can use code completion after @var tag to obtain all available types from the project in the code completion.