corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

NetBeans IDE for PHP Editor: Brief Overview

This document is a brief overview of the main features of the NetBeans IDE PHP Editor.

Contents

Content on this page applies to NetBeans IDE 6.7

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE PHP download bundle
A PHP engine Version 5
A web server Apache HTTP Server 2.2 is recommended.
A PHP debugger XDebug 2.0 or later

Getting Ready

To successfully develop and deploy PHP applications in the NetBeans IDE for PHP, you need to have all the required software installed and configured for PHP development.

The NetBeans IDE for PHP Editor Layout

The editor contains the following windows:

  • The Projects window shows a list of currently existing projects
  • The Files window shows the files for each project
  • The Services window shows lists of currently available external services such as databases.

You can also open a Navigator window in the left-hand panel by pressing Ctrl + 7.

Syntax Highlighting

The editor provides syntax highlighting for PHP, HTML, JavaScript, and CSS code blocks.

The following syntax elements are highlighted in the current version:

  • PHP keywords
  • PHP variables
  • PHP constants
  • HTML tags
  • HTML input form attributes
  • The current line is displayed with a light-blue background.
  • By double-clicking a PHP variable, a function, or a class, all the occurrences of the variable are highlighted in olive green.
  • Lines where errors have been detected are underlined in red.
The NetBeans for PHP editor window shows a fragment of code with both PHP and HTML code elements highlighted.
To change the settings for highlighting, choose Tools > Options and switch to the Fonts&Colors tab. Specify the settings of your choice.

Go To Declaration

The Go To Declaration function navigates the user from an occurrence of a variable to the line where the variable is declared or initialized. To use this functionality, position the cursor on the relevant variable occurrence and choose Navigate > Go to Declaration from the context menu. or press CTRL + B.

Navigator

The Navigator helps the user to familiarize themselves with the code by displaying code structures as elements of a list that shows the PHP structures of the currently active file. If the file contains any other text sources (for example, HTML) the list displays their structures too. By double-clicking a class, a function, or a variable in the list, the cursor points at the declaration of the clicked element.

The Navigator list does not normally contain any embedded lists, but for complex statements it may contain embedded structures. For example, a PHP class declaration statement can be presented as a tree of declared class members.

The list is dynamically updated while the user types the code.

The Navigator window is shown in the bottom of the left-hand panel.

  1. To open the Navigator window, press CTRL + 7.
  2. From the Navigator drop-down list, choose Navigator. The tree that displays the hierarchy of the HTML and PHP structures appears.
  3. To move to the desired element, double click the relevant node in the list.
    The figure shows an example of using the navigation tool. The figure also shows folded and unfolded classes and functions.

Code Folding

The editor enables the user to selectively hide and display modules, classes or functions of the currently edited file with PHP source code. To fold or display a piece of code, click the - or + icon to the left of the code.

Smart Indent

The editor automatically formats the code while the user is typing.

Formatting

To format a piece of code to make it more understandable, perform the following steps:

  1. Select the relevant code passage.
  2. From the right-mouse context menu, choose Format or press Alt + Shift + F

Bracket Completion

The editor automatically adds and removes matching brackets and quotes while the user is typing the code.

  • Paired single ' ' and double quotes "" "", braces (), and brackets [] are added when the user has typed the first symbol.
  • At the end of a line, a semicolon is added in addition to the closing single or double quote.
  • The closing curly brace is added after the user presses Enter, which also activates the Smart Indent function.
  • Deleting the opening single or double quote, brace, or bracket causes automatic removal of the corresponding closing symbol but does not affect the line end semicolon.
  • When the cursor points at a closing brace ), a closing curly brace }, or a closing bracket ], the paired opening symbol is highlighted in yellow.
    When the cursor points at a closing brace or bracket, the paired opening element is highlighted.

Instant Rename

You can rename a variable, function, etc inside a php file. Place the cursor on a name you want to change and press Ctrl-R. All instances of that variable or function name are highlighted.

PHP code editor after Ctrl-R pressed

Change one instance of the name and all other instances are changed simultaneously

PHP code editor showing a refactored variable name

Code Completion

Code Completion is a common name for a set of features that facilitate and speed up the process of coding. The Code Completion functionality is still under development and the full range of possibilities will be available in future versions of NetBeans.

The following types of code completion are distinguished:

Snippets

Snippets enable the user to generate code for various elements automatically.

  1. Choose Tools > Palette > HTML/JSP Code Clips. A palette containing various user interface elements appears in the right-hand panel.
  2. Drag the required icon on the palette to the relevant position in the code. A dialog box for specifying the parameters of the corresponding elements appears. Fill in the data.
  3. The code that displays the chosen element is generated and inserted in the chosen location.

Context-Sensitive Proposals

The editor provides context-sensitive proposals for completing any number of starting symbols of:

  • A PHP keyword (for example, if, else, elseif, while, switch, function, and so on)
  • A PHP built-in function ( for example, substr, count, and so on)
  • A pre-defined or user-defined variable

The editor not only suggests expansions but also provides parameter hints. To apply Code Completion:

  1. Type the starting symbols of the required character string.
  2. Press CTRL + Space. A dropdown list shows the context-sensitive proposals. Each proposal is supplied with a description and parameter hints. The contents of the list change as you continue typing.
  3. To obtain a list of the PHP key words that are available in the current context, press CTRL + Space without any previous typing.
    A list of context-sensitive proposals. Each proposal is supplied with a description and parameter hints.
  4. To obtain a hint on a variable, just type the dollar symbol "$". A list of all the currently available local and global variables appears.
    A list of variables opens after the user types $. The list contains PHP global variables and variables defined by the user.

Abbreviations

In the current context, the term ""abbreviations" refers to a set of predefined character strings that correspond to the key words used in a programming language. Each abbreviation is associated with an expanded text which contains the full key word and a code template for the key word with parameter hints. To apply this functionality, type an abbreviation and press Tab.
The abbreviation "cls" for the key word "class" is typed
The abbreviation is replaced with the corresponding key word and the code template for the key word is provided.
The full text for the abbreviation is displayed with a code template for class definition
To view the list of defined abbreviation with code templates:
  1. Choose Tools > Options > Editor > Code Templates.
  2. From the Language drop down list, select PHP. The list of PHP abbreviations and code template defined for them is displayed.
  3. To add or remove a definition from the list, use the New or Remove buttons respectively.
  4. To edit a definition, select the relevant row and edit the text in the edit field below the list.
    The list of sbbreviations and code templates defined for them

Code Completion for Constructors

After the new keyword, the code completion window is displayed with constructors and parameters for all available classes in the project.

Code completion hints for a constructor

SQL Code Completion

SQL code completion displays when a string begins with the SQL keyword "select." The first step is to select the database connection.

Code completion offering database connection

All database connections registered with the IDE are displayed.

All database connections registered with the IDE

After you select the connection, SQL code completion offers all tables from that database connection.

All tables in the wishlist db connection

If the table has columns, those are displayed as well.

Columns shown for table

SQL code completion also works with table aliases.

SQL code completion showing tables in hotel alias

PHP 5.3 Namespaces

Code completion supports fully qualified, partially qualified, and unqualified namespace names from PHP 5.3. For more information, please see our screencast.

Parameter Hints

The editor prompts the user regarding the formal parameters of a function or a method in the context where the function or method is called.

  1. Type the starting characters of the function you want to call.
  2. Press CTRL + Space. A dropdown list shows the context-sensitive proposals with the formal parameters for each proposal.
    List of context-sensitive hints
  3. Choose the relevant proposal and press Enter. The name of the chosen function is inserted in the code and a template for entering the parameters is shown in brackets.
    Parameter hints for the chosen context-sensitive proposal.

Defining Variable Type in Comments

You can define a variable and its type in a comment. The comment has to be in the format /* @var $variable type */ . If the comment is written correctly, the var tag is in bold font.

Variables declared in comments

You can use the code template vdoc, followed by Tab, to generate a comment that defines a variable. The variable name is selected, and you can change it. Then press Tab again, and the type is selected.

Variable declaring comment generated by vdoc template

The code template automatically sets the variable name and type. If a variable is used after the place where you insert the template, then that following variable name is suggested by default. If there is not any variable used after the place where you insert the template, but there is a variable used above the template, then that preceeding variable name is suggested as default. If NetBeans IDE is not able locate any variable near where you use the template, then the default name is variable.  The variable type is set automatically according to the same rules.

Variable declaring comment generated by vdoc template, with default variable names and types

Error Messages

The editor analyzes the syntax of the code while you type, and marks syntax errors by underlining the erroneous lines and setting a red bullet next to them. To get an explanation of the error, mouse over the erroneous line or click the red bullet. A tooltip appears with a short explanation of the error.
Error-messaging: a tooltip above the erroneous line.

PHPUnit and Selenium Tests

NetBeans IDE for PHP supports PHPUnit automated tests. Thanks to PHPUnit, NetBeans IDE provides code coverage for PHP, similar to the code coverage the IDE provides for Python and Ruby. Test output appears in the same feature-rich output window that the IDE's JUnit, Ruby, and Python test runners use.

NetBeans IDE also supports the Selenium portable test framework, in combination with PHPUnit. A Selenium plug-in is available from the Update Center. Installing this plugin adds a Selenium server to the IDE's registered servers and adds Selenium test options to the PHP menus.

See Testing with PHPUnit and Selenium for more information.

Symfony Framework Support

Symfony framework support is built-in to the PHP editor. It recognizes existing Symfony frameworks, and you can add the framework to new projects in the last panel of the new PHP project wizard.

In a PHP project with Symfony support, you can run Symfony commands from the project's context menu.

Context menu showing Symfony commands

Use the UI for the commands to:

  • Filter the Symfony commands (1)
  • Specify command parameters (2)
  • Select a command (3)
  • See the command's context help (4)
  • Preview the entire command that will be run (5)
Annotated Symfony command dialog

The list of commands is project specific. This means that if one uses e.g. Doctrine instead of Propel, no Propel commands are listed, just the Doctrine ones. Also, all the commands from the project's Symfony plugins are available.

Code completion includes Symfony variables in Symfony views.

Symfony variables in code completion

The IDE also provides navigation shortcuts between Symfony views and actions.

For more about our Symfony framework support, please see the Symfony screencast.



To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE PHP development features, join the mailing list.

Back to the PHP Learning Trail