NetBeans has introduced a very powerful development environment for PHP since version 6.5. It has some features that make development even easier. Until NetBeans has launched its 6.8 version, it introduced many features for PHP developers. Here is some features:
Code templates is a very powerful feature that can save a lot of time. This templates enables the developer to write few characters which can be expanded to language expressions. Netbeans comes with an excellent group of expressions, but you can add your own templates. Go to tools > options then in the editor section choose Code templates.
Some useful templates can be:
- cls: initialize a class with a contructor.
- fnc: expands to a function.
- fore: foreach statement (
foreach($array as $variable))
- forek: foreach statement with a key (
foreach($array as $key => $var))
to expand an abbreviation press tab, you can set expand trigger also in the code templates editor.
Extend code templates with Zen Coding
PHP is usually combined with HTML, although it should only be simple statements in view files if you are using MVC pattern. Zen coding project offers a set of code templates for rapid HTML coding.Unfortunately NetBeans didn’t get the full power of Zen coding yet, it only provides code templates .
Download Zen coding for NetBeans you will get a ZIP file. Use the code template editor and click ‘Import’ and select this file, then restart NetBeans. Now you get a powerful HTML snippets for NetBeans. You can get a list of all snippets here.
Useful code templates trick, ‘surround with’
Some code templates in NetBeans, and in the template provided with Zen Coding, can be used to surround some text with a code template. This can be useful if you forget, or needed later, to surround some code with certain code template. An example for this is if conditions, loops, ….etc. Those code templates can be used if you select the required code. You will find a small yellow light bulb on the left side of the code, click this bulb to show a list of those templates.
Introduce a variable
NetBeans is very smart when it scans for variable types. It utilizes comments, specially PHPDoc. Variable types is very useful when the IDE displays code completion. But sometimes when you are working on many files many IDEs and code editors doesn’t identify this variable types so the user will miss the code completion feature of the IDE, which saves a lot of time while coding. To retrieve code completion feature again. You can tell NetBeans about the variable type yourself using the following method.
/* @var $variable VariableType */
This will tell NetBeans about the variable you are using and if it is an instance of some class NetBeans will show code completion when required. This can be great if you are using a PHP framework that uses MVC pattern. Personally I use this technique to acquire code completion for CakePHP helpers,
Even I could use it to get completion for
$this in views like this:
/* @var $this View */
Editor folds is a way to group some code in a fold and give them a label. Editor folds looks like this:
to create an editor fold with following code:
//<editor-fold defaultstate="collapsed" desc="your description here ..."> // your code here .... //</editor-fold>
You can ignore
defaultstate="collapsed" to make the code visible just after you open the file. Please take care of order and case of syntax.