Useful NetBeans 6.8 – PHP tips

Bookmark and Share

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

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.

Code templates editor in netbeans php

Code templates editor in NetBeans PHP editor

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.

NetBeans code templates surround with

NetBeans code templates surround with dialog

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,

/* @var $html HtmlHelper */
/* @var $javascript JavascriptHelper */
/* @var $form FormHelper */

Even I could use it to get completion for $this in views like this:

/* @var $this View */

Editor folds

Editor folds is a way to group some code in a fold and give them a label. Editor folds looks like this:

Editor folds in netbeans

Editor folds in NetBeans

to create an editor fold with following code:

//<editor-fold defaultstate="collapsed" desc="your description here ...">
// your code here ....

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.

15 comments on “Useful NetBeans 6.8 – PHP tips

  1. Hi, thanks for these tips. Just a little request though: “Introduce a variable” is not so clear. Could you make a little video to show how they works? Thanks a lot.

    • It is very easy you can tell NetBeans about the type of the variable you are using. It is useful if you are using many files and NetBeans couldn’t determine the type of a variable so you can tell NetBeans yourself using this method.

  2. Pingback: Tower Of Power - Netbeans Code Completion and your Zend_View

  3. Pingback: Webs Developer » Daniel Cousineau’s Blog: Netbeans Code Completion and your Zend_View

  4. How to code template variables transform uppercase|lowercase?

    I need to convert same variables to uppercase|lowercase|captalize.

    * @package ${1 default=”Hello”}
    * @subpackage ${com}_${1 capitalize=false}
    * @copyright Copyright (C) 2012 ${AtomTech}, Inc. All rights reserved.
    * @license GNU General Public License version 2 or later; see LICENSE.txt

    // No direct access
    defined(‘_JEXEC’) or die;

    How can I do this?

Leave a Reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>