php – How can I get Netbeans to use import statements instead of fully qualified class names on autocomplete?-ThrowExceptions

Exception or error:

I’ve moved from PDT to Netbeans recently, and there is something I can’t figure out. How do I get the IDE to use ‘use’ statements at the beginning of a file on autocompleting class names?
Like:

use \Foo\Bar\Baz;

class Something extends Baz { /* ... */ }

Instead, if I use autocompletion I get

class Something extends \Foo\Bar\Baz { /* ... */ }

I’ve checked the code completion settings, but the most promising option (smart) wanted me to first write the imports in order to use autocompletion for short class names. PDT was capable writing that line to the beginning of the file.

How to solve:

In newer versions of Netbeans (i.e. 7.3+), you can press Ctrl+Shift+I (Alt+Shift+I won’t work as expected, though).

First, it searches for any smart or fully qualified instances in your file and then asks you of what types the found ones should be. It then adds fully qualified use statements to the top of your file and changes existing instances in the source to unqualified.

Answer:

I use ‘Unqualified’, then go to the top of the document and type ‘use ‘ and use the autocomplete dropdown to select the class, the fully qualified class is inserted.

The other option is to use ‘Fully Qualified’ and then copy->paste it in to a new use statement.

I contacted netbeans a while ago about it and they agreed that it would be a good feature, but I never followed it up, should I chase it up with them?

Answer:

Go to your Netbeans Preferences, then to Editor tab. In the ‘Language’ Dropdown, select PHP, then scroll to the bottom. The last entry is called Type of Code Completion (PHP 5.3 only) You can choose Smart or Unqualified, and either would most likely solve your issue. (I’m not sure about previous versions, but I use NetBeans 7.0 which is latest stable)

Leave a Reply

Your email address will not be published. Required fields are marked *