21 Developing PHP Applications

This chapter describes developing PHP applications for NetBeans IDE, including editing, debugging, and using continuous build servers.

This chapter contains the following sections:

21.1 About Developing PHP Applications

NetBeans IDE (Integrated Developer Environment) for PHP provides tools for PHP programmers. The core of NetBeans PHP support is the PHP Editor, which includes code completion and other programming aids. NetBeans IDE for PHP can also be integrated with the Xdebug debugger, PHPUnit tester, and PHPDocumentor documentation generator.

Please note that the NetBeans IDE is designed to be as lightweight as possible. It does not contain visual design tools or other heavy-duty features.

21.1.1 Databases

The IDE supports working with databases. Open the Services window and register your database by right-clicking the Databases node. You may need to add the path to your database driver as well. NetBeans IDE provides a tree view of database contents and an SQL editor. For more information, see Section 24.3, "Setting up a Database Connection."

21.1.2 Remote Development

The IDE includes support for Subversion, Mercurial, and Git versioning systems. You can also add CVS support as a plugin. For more information, see Section 3.1, "About Versioning Applications with Version Control."

The IDE also provides basic support for FTP/SFTP, sufficient for a lone developer to work on a simple project. If you are working on a complicated project or with multiple developers, you should use a version control system. If you must use FTP/SFTP with a complicated project, consider using the IDE in combination with a full-featured FTP client.

21.1.3 Frameworks

The IDE supports some PHP frameworks. You can add support for a framework in the IDE by installing the plugin for the framework in the Plugins Manager. You can configure the global options for the framework in the Frameworks & Tools tab of the PHP category in the Options window.

After you install the plugin you can add a framework to a project when you create the project by selecting the framework in the last page of the New PHP Project wizard. If a project includes framework support, that framework's commands are available in the project's context menu in the Projects window.

21.2 Working with PHP Applications

Before you begin to work with PHP applications in the IDE you need to install and configure your PHP environment. The PHP environment includes:

  • A PHP engine

  • A web server, such as Apache

  • Optionally, a database, such as MySQL or Oracle Database

  • Optionally, a debugger supported by the IDE

21.2.1 How to Create a PHP Project

A project is a group of source files and the settings with which you run and debug those source files. In the IDE, PHP development takes place within a project, though you can also edit, run and debug a PHP file that is not in a project.

The IDE provides a wizard that enables you to create a new empty PHP project or to create a PHP project from existing sources that are located on your local system or on a remote server. The IDE provides basic support for FTP and SFTP, however, it is preferable to use version control with remote projects and use a version control system to commit your changes to the remote server.

By default the IDE places all project files in your web server's public folder (such as /htdocs). The IDE reads the location of your web server from your PHP environment. However, you have other options for file locations. These options appear in the New Project wizard:

  • Have the NetBeans metadata stored in another folder.

  • Copy the source files to another location. With this option, you could keep the PHP project in one location (such as the same folder as your Java projects) and the IDE could copy it to the public folder of your web server.

To create a new empty project:

  1. Open the New Project wizard and select the PHP project category.

  2. Select the PHP Application project template. Click Next.

  3. Specify the project name and location of the project and files. Click Next.

  4. Select an option in the Run As drop-down list to create the default run configuration for the project and specify any required details. Click Next

    You can modify the run configuration and create new run configurations in the Project Properties window.

  5. Select any PHP frameworks that you want to use in your project

    You can add support for PHP frameworks and Composer by installing plugins in the Plugins Manager. If you are using Composer you can click Next to provide Composer details.

  6. Click Finish.

To create a project from sources on a remote server:

  1. Open the New Project wizard and select the PHP project category.

  2. Select PHP Application from Remote Server as the project type. Click Next.

  3. Specify the project name and location for the local sources. Click Next.

  4. Specify the URL of the remote server.

  5. Select a connection from the Remote Connection drop-down list

    If the remote connection does not exist, click Manage to open the Manage Remote Connections dialog box where you can create, edit and delete remote connections. The remote connections that you create are also available when you create a run configuration that targets a remote web server.

  6. Specify the directory on the remote server that contains the sources. Click Next.

    When you click Next the IDE attempts to connect to the remote server.

  7. Click Finish.

21.2.2 How to Create a Run Configuration for the Project

Every NetBeans PHP project has at least one run configuration. You set this default configuration when you create the project. You can add as many additional run configurations as you want in the Project Properties window. In a run configuration, you can select any of the following Run As options:

  • As a local web site, on your local web server (default)

  • As a remote web site, over FTP/SFTP. You can also set up remote synchronization.

  • As a command-line script

You can switch between run configurations at any time by right-clicking the project's node and selecting Set Configuration and the configuration in the popup menu.

To create a run configuration:

  1. Open the Project Properties window and select the Run Configuration category in the left pane.

    Alternatively, right-click the project node in the Projects window and choose Set Configuration > Customize in the popup menu.

  2. Click New next to the Configuration drop-down list and type a name for the new run configuration. This name will be used in the Set Configuration popup menu to identify the configuration.

  3. Select a Run As option from the drop-down list.

  4. Specify any options and details required by the Run As option that you select.

    The required details for the run configuration will change according to the Run As option that you select. For example, if you select Remote Web Server you must provide the connection details for the remote server.

  5. Click OK.

21.2.3 How to Synchronize Local and Remote Sources

You can synchronize the files on your local file system with the files that are located on a remote server. To synchronize the contents of a local folder with the contents of a folder on a remote server you must set the run configuration to a configuration where the Run As option is set to Remote Web Site and you must be able to connect to the remote server with the specified connection details.

Remote Synchronization is a feature of PHP projects set to run as Remote Web Site, over FTP or SFTP. Remote Synchronization allows you to compare your local version of a project with the version on a server and to diff, upload, or download files so the local and remote versions of the project match. The Remote Synchronization dialog box opens from the popup menu (right click) of the Source Files folder of a PHP project.

The IDE can perform one of several operations when synchronizing a file. The table in the Remote Synchronization dialog box displays the operations that the IDE will perform and the files that will be affected during synchronization.

To synchronize local and remote sources:

  1. Set the run configuration of your project to a configuration where the Run As option is set to Remote Web Server.

  2. Right-click the Source Files node in the Projects window and choose Synchronize in popup menu.

  3. Select the operations to perform during the synchronization.

  4. Modify the operations for specific files by selecting the files in the table and using the buttons below the table to modify the operations for the selected files.

  5. Click Synchronize.

21.2.4 How to Run a Project

Every NetBeans PHP project has at least one default run configuration that you set when you create the project. When you run the PHP application the IDE performs actions that are based on the current run configuration. You can switch between run configurations to easily change the actions that the IDE performs when you run the application. For example, you can create one run configuration that deploys the application to your local server and a second run configuration that deploys to a remote staging server. You can switch between run configurations at any time by right-clicking the project's node and selecting Set Configuration and the configuration in the popup menu.

To run a PHP application:

  1. In the Projects window, right-click the project node and select Set Configuration > <default> in the popup menu.

    The <default> configuration is the configuration that you created when you created project. Any other run configurations that you created are also listed in the Set Configuration sub-menu.

  2. Right-click the project node in the Projects window and choose Run.

    Depending on your run configuration, the IDE might open a web browser window to display a page in your project or run your project as a script using a PHP interpreter. If the project is configured to run as a command-line script, you have to run the application from the command line.

21.2.5 How to Test a PHP Project

You can use PHPUnit and Selenium to test a PHP project.

To test a PHP project:

  1. Open the Project Properties window and select the Run Configuration category in the left pane

    Alternatively, right-click the project node in the Projects window and choose Set Configuration > Customize in the popup menu.

  2. Click New next to the Configuration drop-down list and type a name for the new run configuration. This name will be used in the Set Configuration popup menu to identify the configuration.

  3. Select a Run As option from the drop-down list.

  4. Specify any options and details required by the Run As option that you select.

    The required details for the run configuration will change according to the Run As option that you select. For example, if you select Remote Web Server you must provide the connection details for the remote server.

  5. Click OK.

21.3 Editing PHP Files

The IDE contains a number of features that simplify editing PHP files, including syntax highlighting, code completion, using templates, and more. This section describes these features.

21.3.1 How to Use 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.

To change the settings for highlighting, choose Tools > Options and switch to the Fonts&Colors tab. Specify the settings of your choice.

21.3.2 How to Use Code Completion

Code Completion is a common name for a set of features that facilitate and speed up the process of coding.

The following types of code completion are provided:

  • Snippets

  • Context-sensitive proposals

  • Abbreviations

  • Code Completion in Constructors

  • SQL Code Completion

  • PHP Namespaces

  • Overridden and Implemented Methods

  • Annotations

  • PHP Traits

21.3.2.1 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.

21.3.2.2 Context-Sensitive Proposals

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

  • 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 drop-down 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.

  4. To obtain a hint on a variable, type the dollar symbol "$". A list of all the currently available local and global variables appears.

21.3.2.3 Code Templates and 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 is replaced with the corresponding key word and the code template for the key word is provided.

To view the list of defined abbreviations 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.

For more information, see the following entry on the PHP blog: https://netbeans.org/kb/docs/php/code-templates.html.

21.3.2.4 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.

21.3.2.5 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.

All database connections registered with the IDE are displayed. After you select the connection, SQL code completion offers all tables from that database connection. If the table has columns, those are displayed as well.

SQL code completion also works with table aliases.

21.3.2.6 PHP Namespaces

Code completion supports fully qualified, partially qualified, and unqualified namespace names. The IDE also helps you resolve missing namespace use statements. Inside a namespace where you want to fix missing use statements, either right-click and select Fix Uses..., or press Ctrl+Shift+I and go to Source > Fix Uses... A dialog opens offering fully qualified names for each needed use statement. For more information, see the following entry on the PHP blog:

https://blogs.oracle.com/netbeansphp/entry/how_to_fix_your_use

21.3.2.7 Overridden and Implemented Methods

Code completion between class members offers to override or implement methods.

21.3.2.8 Annotations

NetBeans IDE code completion supports the following types of PHP annotations:

  • ApiGen (legacy PHPDoc annotations)

  • PHPUnit

  • Doctrine 2 (ORM and ODM)

  • Symfony 2

Every annotation can be associated with a context. NetBeans IDE recognizes four contexts:

  • function

  • class/interface (type)

  • method

  • field

You can add more annotations to code completion in Tools > Options > PHP > Annotations.

21.3.2.9 PHP Traits

Traits are a mechanism of code reuse introduced in PHP 5.4.0. The IDE recognizes traits syntax and code completion includes methods, functions, and anything else inherited from or declared in a trait. For more information, see the blog post at https://blogs.oracle.com/netbeansphp/entry/php_5_4_support_traits.

21.3.3 How to Use 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.

21.3.4 How to Use 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.

To use parameter hints:

  1. Type the starting characters of the function you want to call.

  2. Press Ctrl+Space. A drop-down list shows the context-sensitive proposals with the formal parameters for each proposal.

  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.

21.3.5 How to Use the Go To Declaration Feature

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.

21.3.6 How to Use Rename Refactoring and Instant Rename

Rename Refactoring lets you rename an element such as a class name across all files in a project. The feature forces you to preview your changes before you can make them. The preview window shows you every location of the element and lets you exclude individual occurrences of the element from being renamed.

Instant Rename is a simpler function that only works in "non-public" contexts, such as renaming a variable inside a method, or renaming private variables and fields. Instant Rename lets you only rename an element within a file and does not provide a preview window.

To use Instant Rename, place the cursor on a name you want to change and press Ctrl+R. If Instant Rename applies to that variable, all instances of that variable or function name are highlighted. Change one instance of the name and all other instances in the file are changed simultaneously

To use Rename Refactoring, select the element you want to rename and either press Ctrl+R or right-click and select Refactor > Rename. A dialog opens for you to rename the element. Rename the element and press Preview. The Refactoring window opens. In this window, you can find every instance of the element in your project and decide whether or not to rename it.

21.3.7 How to Use Code Generators

When you press the combination Alt+Insert (Ctrl+I on Mac), a menu opens with all possible code generators. The list of generators is context sensitive. It depends on the position of the caret in the code when the key combination is pressed. Depending on your position in the code, you can generate a database connection, database tables, lorum ipsum text, and several others. This section describes only the following code generators:

  • Constructors

  • Getters and Setters

  • Overridden and Implemented Methods

21.3.7.1 Constructors

You can generate constructors by pressing Alt+Insert (Ctrl+I on Mac) when the caret is inside a class body, but not inside any functions contained in that body. When you select Generate... Constructor, a dialog opens listing the fields you can initialize by the constructor. The field names are used as parameters of the constructor.

You can decide not to select any fields. In this case, the IDE generates an empty constructor, with no parameters. If the field is a class with properties, you can either select individual properties, or you can select the class, in which case all the class' properties are selected automatically.

For more information, see the following NetBeans PHP blog post: https://blogs.oracle.com/netbeansphp/entry/generate_constructor_getters_and_setters.

21.3.7.2 Getters and Setters

You can generate getters and setters by pressing Alt+Insert (Ctrl+I on Mac) when the caret is inside a class body and selecting Getter, Setter, or Getters and Setters. Only the possible functions are displayed. For example, if you already have setters for the available properties, only the getter option appears.

When you select Generate... Getter/Setter/Getter and Setter, a dialog appears with the properties for which you can generate a getter or setter. The properties are displayed in a tree. If you select a parent class, you automatically select all that class' properties.

You can name a getter or setter according to either the convention getName or the convention get_name.

For more information, see the following NetBeans PHP blog post: https://blogs.oracle.com/netbeansphp/entry/generate_constructor_getters_and_setters.

21.3.7.3 Overridden and Implemented Methods

You can generate overridden or implemented methods by pressing Alt+Insert (Ctrl+I on Mac) when the caret is inside a class declaration and there are multiple class members. A dialog opens showing the methods you can insert and indicating whether they are overridden or implemented.

This feature complements the "Implement all abstract methods" hint and code completion for overridden and implemented methods.

For more information, see the following NetBeans PHP blog post: https://blogs.oracle.com/netbeansphp/entry/generate_overriden_implemented_methods

21.3.8 How to Define 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.

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.

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 preceding variable name is suggested as default. If the 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.

For more information, see the following screencast: https://netbeans.org/kb/docs/php/php-variables-screencast.html

21.3.9 About PHP 5.4 Support

NetBeans Editor for PHP offers a number of features specific to developing with PHP 5.4, discussed in detail in the following blog entries:

21.4 Debugging PHP Applications

Debugging is the process of examining your application for errors. The process of debugging is accomplished by setting breakpoints and watches in your code and running it in the debugger. This enables you to execute your code one line at a time or breakpoint-to-breakpoint and examine the state of your application in order to discover any problems.

When you start a PHP debugging session, a browser window opens. By default the application stops at the first line of the program. You must switch back to the IDE to continue the debugging session. You switch back and forth between the IDE and your browser throughout your debugging session.

Additional debugger windows also appear automatically at the bottom of your screen. These windows help you keep track of breakpoints, variables, sources, the call stack, and threads.

You can also debug applications that are running on a remote machine by setting up path mapping to a custom URL.

For more details see the following document: https://netbeans.org/kb/docs/php/debugging.html

21.4.1 How to Debug a PHP Project

The following is a typical work flow for debugging a PHP project.

To debug a PHP project:

  1. Set up XDebug on your local system.

  2. Set breakpoints and watches in your code.

  3. Start the debugging session.

  4. Step through your code.

  5. Finish the debugging session.

The following sections show in greater detail the steps for setting up XDebug and debugging your project.

21.4.2 How to Set Up XDebug

The IDE supports the XDebug third-party PHP debugger. If XDebug is installed correctly and the PHP environment is configured correctly, you can debug PHP projects inside the IDE. You can also debug PHP files remotely if you set up path mapping.

When you run XDebug from NetBeans IDE, PHP program execution pauses at every line where you set a breakpoint. When the program execution is paused, XDebug can retrieve information about the current program state, such as the values of the program variables.

Additional information can be found in the following wiki document:

http://wiki.netbeans.org/HowToConfigureXDebug

21.4.2.1 Check If XDebug Is Installed

The first step you should take is to see if XDebug is already installed on your system. Some AMP packages come with XDebug. Run phpinfo(). If there is an XDebug section in your PHP info, XDebug is installed. If XDebug is installed, see Section 21.4.2.3, "Setting Up XDebug."

21.4.2.2 Install XDebug

The NetBeans IDE for PHP community consensus is that the XDebug installation wizard should be used to install XDebug.

  1. Run phpinfo().

  2. Copy the results of phpinfo() from your browser into the clipboard.

  3. In a browser window, open the following URL: http://xdebug.org/wizard.php

  4. Paste your PHP info into the XDebug wizard form and click Analyze.

  5. Follow the instructions that appear.

21.4.2.3 Setting Up XDebug

The following section assumes you have XDebug installed and you need to set it up.

  1. Run phpinfo().

  2. In the PHP info, find the entries for Loaded Configuration File and Additional .ini Files Parsed.

  3. For each php.ini or xdebug.ini file you find, open that file and perform the following steps:

    1. Make sure the file contains the following settings:

      zend_extension=[path to xdebug .so or .dll]
      xdebug.remote_enable=1
      xdebug.remote_handler=dbgp
      xdebug.remote_mode=req
      xdebug.remote_host=127.0.0.1
      xdebug.remote_port=9000

      These settings are usually in a section labeled [XDebug]. If such a section does not exist, create one. However, some AMPs may differ. For example, in some versions of XAMPP AMP, the line zend_extension = "G:\xampp\php\ext\php_xdebug.dll" is outside of the [XDebug] section, and the name of the .dll file is nonstandard (php_xdebug.dll instead of xdebug.dll).

    2. Comment out all entries for Zend Optimizer, Zend Studio Debugger, or other Zend extensions.

21.4.2.4 Testing XDebug

Before trying out XDebug from the IDE you should test your XDebug installation from the command-line. One way to test XDebug is to write a test in PHP and run a debug session on it.

  1. You need an arbitrary PHP file. If you do not have one, create a file called index.php, put any code into it and save it.

  2. Using a text editor, create a file called dbgtest.php and type or paste the following code into the file (save the file afterward):

    <?php
    $address = '127.0.0.1';
    $port = '9000';
    $sock = socket_create(AF_INET, SOCK_STREAM, 0);
    socket_bind($sock, $address, $port) or die();
    socket_listen($sock);
    $client = socket_accept($sock);
    echo "Connection established: $client";
    socket_close($client);
    socket_close($sock);
    ?>

  3. Run the code from the command line (for example, /usr/bin/php dbgtest.php).

  4. Start a debug session for an arbitrary PHP file from within a browser window (for example, http://localhost/index.php?XDEBUG_SESSION_START=mysession).

If XDebug is properly configured, the script started in step 3 should print a message similar to "Connection established: Resource id #5". If no message is printed and the script is still running, XDebug is not properly configured or uses a different port or has another problem. In this case, kill the running process and investigate where the problem is.

21.4.2.5 Setting Up Debugging Options in NetBeans IDE

After you install, configure, and successfully test XDebug, you can set up XDebug options in the IDE. To learn more about these options, open Tools > Options (NetBeans Preferences on Mac), go to the PHP category, select the Debugging tab, and click Help.

21.4.3 How to Set PHP Breakpoints in the IDE

A breakpoint is a flag in the source code that tells the debugger to stop execution of the program. When your program stops on a breakpoint, you can perform actions like examining the value of variables and single-stepping through your program.

21.4.3.1 Setting Breakpoints

To set a breakpoint for a line in your PHP code, left-click the left margin or line number of the line. To remove the breakpoint, left-click the breakpoint icon.

To temporarily disable a breakpoint, right-click a breakpoint badge and deselect Breakpoint > Enabled in the popup menu. This toggles the breakpoint into a disabled state and the breakpoint badge in the left margin is now grey.

The Source Editor indicates a line breakpoint by highlighting the line at which the breakpoint is set in red and placing an annotation in the left margin. Users of other languages supported in the IDE may be familiar with other types of breakpoints but only line breakpoints are supported in PHP projects. For a table of breakpoint annotations that are used in the editor, see Section 10.9.4, "Managing Breakpoints."

You can view and organize all IDE breakpoints by choosing Windows > Debugging > Breakpoints (Alt+Shift+5). For details on organizing breakpoints, see Section 10.9.4.3, "How to Organize Breakpoints Into a Group."

21.4.4 How to Set the Current Context in the PHP Debugger

The current context is the portion of your program on which the debugger is currently focusing. When multiple sessions are running, only one session is current. Within the current session, the thread from which the debugger regained control is the default current thread. Inside the current thread, the most recent call is the default current call.

You can make any session, thread, or call current by right-clicking its node in the appropriate debugger window and choosing Make Current.

21.4.4.1 Debugger Windows and Context

Most debugger windows depend on the current context. When you change the current context, the contents of these windows are updated to reflect the new context.

For example, the Debugging window shows the threads in the current session, while the Call Stack window shows the call stack for the current thread. The Variables window shows the variables that are local to the current call.

The exceptions are the Breakpoints and Watches windows. These windows list all breakpoints and watches set in the IDE. While the set of watches is shared by all sessions, an individual watch expression is evaluated and displayed based on the current context.

21.4.4.2 The Source Editor and Context

When a variable is active in the current context, the Source Editor displays the value of the variable when you move the pointer over it. In cases where a program includes different variables with the same name, the Source Editor displays the value based on the current context, and not on the instance of the variable in the source code.

21.4.5 How to Start a PHP Debugging Session

PHP debugging sessions use debugger windows inside the IDE. You can debug projects, or you can remotely debug Web pages.

21.4.5.1 Starting a PHP Debugging Session

The IDE starts the debugger, then runs the application inside the debugger. When you start a debugging session, the IDE automatically opens the debugger windows, opens a browser window, and prints debugger output to the Output window. You start the debugging session in the browser window.

By default, the debugging session stops at the first line of the PHP code. You can disable stopping at the first line in the Debugging tab in the PHP category of the Options window. When the session stops, you have a number of options for stepping through/into or continuing the debugging session.

To start debugging a project:

  1. Locate the project's node in the Projects window.

  2. Either right-click the project node and select Debug, or select the project node and run the Debug Project command (Ctrl+F5).

Alternatively, you can debug an individual PHP file:

  1. Expand the project's node in the Projects window. Navigate the tree until you locate the file you want.

  2. Either right-click the file node and select Debug, or select the file node and run the Debug File command (Ctrl+Shift+F5).

The process for starting a debugging session is the same for local and remote projects. However, path mapping must be set up to debug remote projects.

21.4.5.2 Remote PHP Debugging and Path Mapping

It is possible to debug both scripts and web pages, and web pages can be debugged either locally or remotely. For Remote Debugging, unfortunately the debugged PHP file on the remote server is not the same as the file opened in the IDE running on a local machine. Debugger support in the IDE must thus be able to map server paths to local paths. However, due to many complications, path mapping cannot be resolved automatically for every individual scenario. Therefore, you must manually define path mapping through the project setup for individual run configurations. You can also specify the proxy server, if any, and the URL at which the debugging session starts. If you do not specify this URL, debugging starts at your index file.

To set up path mapping and enable custom debugging URLs:

  1. Right-click the project's node in the Projects window and open the project's Properties from the context menu.

  2. In the Project Properties dialog, go to the Run Configuration category.

  3. Click the Advanced button. The Advanced Web Configuration dialog opens.

  4. Add the server path and the project path for path mapping.

  5. Under "Debug URL", select one of the following (do not leave the default selected):

    • Ask Every Time, which has the IDE prompt you for the URL when you start a debugging session.

    • Do Not Open Web Browser, which requires you to open the browser and enter the URL manually (you need the GET/POST XDEBUG_SESSION_START variable).

  6. If you are using a proxy server for debugging, enter the server's host name and port in the Debugger Proxy section.

For more information, please see the following post in the NetBeans for PHP blog:

https://blogs.oracle.com/netbeansphp/entry/path_mapping_in_php_debugger

21.4.5.3 Stepping Through Your Program

By default, the debugging session stops at the first line of the PHP code. You can disable stopping at the first line in the Debugging tab in the PHP category of the Options window. When the session stops, you can step through your lines of code using the following commands on the Debug menu or toolbar:

You should set breakpoints before you start a debugging session or you will have to step over/into/out of your entire application. If you disable Stop at First Line and do not set any breakpoints, your application never stops during debugging and you get no useful results.

Command Shortcut Description

Debug > Continue

F5 Runs the program until it reaches the next breakpoint or until the program terminates normally.

Debug > Step Over

F8 Executes one source line. If the source line contains a call, executes the entire routine without stepping through the individual instructions.

Debug > Step Into

F7 Runs the program to the next line and pauses execution before any changes have been made to the state of the program.

Debug > Step Out

Ctrl+F7 Executes one source line. If the source line is part of a routine, executes the remaining lines of the routine and returns control to the caller of the routine. The completed method call is highlighted in the Source Editor.
Debug > Run to Cursor F4 Runs the program to the cursor location in the Source Editor and pauses the program. The file you have selected in the Source Editor must be called from the main class of the main project.

For more details, see the following document:

https://netbeans.org/kb/docs/php/debugging.html

21.4.5.4 Finishing a PHP Debugging Session

If necessary, you can stop the current debugging session using the Shift+F5 shortcut. You can also close a specific debugging session using the Sessions window.

To finish the current debugging session:

  1. Choose Debug > Finish Debugger Session (Shift+F5).

To finish one of several debugging sessions:

  1. Open the Sessions window by choosing Window > Debugging > Sessions (Alt+Shift+6).

  2. Right-click the debugging session you want to stop and choose Finish.

21.4.6 How to Use PHP Debugger Windows

When you start a debugging session the IDE opens some debugging windows by default as tabs below the editor. You can open any debugger window by choosing Window > Debugging > window-name (for example, Window > Debugging > Breakpoints).

Each debugger window displays a variety of icons to relay information about the object. For example, the Breakpoints window uses a small red square to indicate a breakpoint set on a line. Some windows also include a node expansion control to the left of the icon. Clicking this control expands and collapses the object.

In the debugger tabs, information is organized into lists. Each list item represents a single object. Each column represents a property of the object. Data displayed in blue underlined text is linked to the source code.

Some elements of list in the debugger tabs have editable properties, such as the value property of a variable in the Variables window. If you select a property and the property has a white background you can edit the property. A selected property with a gray background cannot be edited.

You can move a column in a debugger tab by dragging the column header. You can click to the right of the column titles to open a dialog box that enables you to choose the columns that are displayed. You can right-click an item in the list to open a pop-up menu where you can choose to perform actions on that item and modify how the items are displayed in the tab.

21.4.6.1 Using the Breakpoints Window

The Breakpoints window lists all the breakpoints that you set in your projects. You can open the Breakpoints window by choosing Window > Debugging > Breakpoints. If you open the Breakpoints window when a debugging session is running, it closes automatically when you end the debugging session. If you open the window when no debugging session is running, it stays open until you close it.

By default, each entry contains a short text description of the breakpoint and a checkbox indicating whether the breakpoint is enabled or disabled. You can enable or disable a breakpoint directly in the Breakpoints window by selecting or deselecting the checkbox.

21.4.6.1.1 Icons

The icon to the left of the breakpoint summary indicates the type of the breakpoint.

Icon Description

Breakpoint icon

Breakpoint set on a specific line.

Invalid line breakpoint icon

Invalid line breakpoint.

Disabled breakpoint icon

Disabled breakpoint.

21.4.6.1.2 Actions

The pop-up menu in the Breakpoints window includes the following items.

Menu Item Description

Go to Source

Finds the location of the selected breakpoint in the source code.

Disable

Turns off the selected breakpoint, but preserves an entry for it in this window.

Move Into Group

You can organize breakpoints into groups so that you can enable, disable, and delete the breakpoints as a group. You can select one or multiple breakpoints and then choose this option to create or add the breakpoints to a group. The group name is displayed as an expandable node in the Breakpoints window in the Custom Group view. You can also choose to display custom groups in the Nested view.

New Breakpoint

Creates a new breakpoint.
Enable All Enables all the breakpoints in this window.
Disable All Disables all the breakpoints in this window.
Delete Deletes the selected breakpoint(s).

Note: If you choose this option, the breakpoint(s) is deleted immediately. There is no confirmation dialog box.

Delete All Deletes all the breakpoints in this window.

Note: If you choose this option, all breakpoints are deleted immediately. There is no confirmation dialog box.


21.4.6.1.3 Grouping Breakpoints

You can modify how breakpoints are organized in the Breakpoints window by clicking the Breakpoint Groups button and selecting a grouping strategy from the popup window. If you choose Nested you can specify the specific groups and the order of the groups that are displayed.

21.4.6.2 Using the Call Stack Window

The Call Stack window lists the sequence of calls made during execution. When the debugger is suspended, the Call Stack window shows the sequence of function calls (that is, the call stack). On initial suspension, the top-most stack frame is selected automatically.

Double-click on a function call in the window to go to that line in the editor. If the call is made to a PHP class, the Navigator window will also go to that line when you double-click the call.

You can double-click on a call stack frame to select it, then explore any variable or expression values for that frame in the Variables and Watches windows.

21.4.6.2.1 Icons

The following table describes the icons displayed to the left of the call name.

Icon Description

Current Call icon

The current call on the call stack.

Call Other Than Current icon

A call other than the current call.

21.4.6.3 Using the Threads Window

The Threads window lists all threads in the current debugging session. You open the Threads window by choosing Window > Debugging > Threads.

The information given for each thread is the thread name, state and if the thread is suspended. One thread is the current thread. By default, the current thread is the thread in the current session from which the debugger gained control. When you select a different current session, the Threads window is updated to show the threads for that session.

21.4.6.4 Using the Variables Window

The Variables window lists the local variables in the current call. By default, the Variables window opens automatically whenever you start a debugging session. You can open the Variables window by choosing Window > Debugging > Variables.

If you open the Variables window when a debugging session is running, it closes automatically when you end the debugging session. If you open the window when no debugging session is running, it stays open until you close it.

The information given for each variable includes the variable name, type, and value. You can click the control to the left of the name to expand or collapse the variable. If the object type is displayed in blue underlined text, clicking the text jumps to the object type in the source code. You can click the Value cell to edit the value directly in the Variables window.

In some cases, the debugger assigns a pound sign (#) and a number as the variable's value. This number is a unique identifier of the given instance. You can use this identifier to determine if a variable points to the same or to a different instance. You cannot edit this value.

21.4.6.4.1 Properties

By default, all properties are displayed directly in the Variables window, except for the toString() property. To display the toString() property in the Variables window, right-click in the window and choose List Options > Change Visible Columns and then select the toString() checkbox in the dialog box.

21.4.6.5 Using the Watches Window

The Watches window lists all variables and expressions that you have specified to watch while debugging. By default, the Watches window opens automatically whenever you start a debugging session. You can open the Watches window by choosing Window > Debugging > Watches.

If you open the Watches window when a debugging session is running, it closes automatically when you end the debugging session. If you open the window when no debugging session is running, it stays open until you close it.

The information given for each watch includes the variable or expression name, type, and value. If the variable has a control to its left, you can click the control to expand or collapse the object. If the object type is displayed in blue underlined text, clicking the text jumps to the object type in the source code. You can click in the Value cell to edit the value directly in the Watches window.

When you add a new variable or expression to the Watches window, the value of the variable or expression is immediately evaluated and displayed. The value of a watch is based on the current context. As you move through your program code, the Watches window is updated to show the value of the watch for that context.

In some cases, the debugger assigns a pound sign (#) and a number as the variable's value. This number is a unique identifier of the given instance. You can use this identifier to determine if a variable points to the same or to a different instance. You cannot edit this value.

21.4.6.5.1 Properties

You can see all the properties for a watch by right-clicking the node for the watch and choosing Properties.

By default, all properties are also displayed directly in the Watches window, except for the toString() property.

21.4.7 How to View Program Information

While debugging PHP applications, you can view two types of programming information:

  • variables, either in the variables window or in the source editor

  • watches, which are mechanisms for observing the changes in the value of a variable or expression during execution.

21.4.7.1 Viewing Variables When Debugging PHP Applications

In the IDE, local variables are listed in the Variables window, however it is also possible to evaluate variables directly in the Source Editor.

Using the Variables Window

For each variable within the current call, the Variables window displays information including the variable name, type, and value. The Variables window also displays all of the static fields from the present class and all superclasses for each variable, as well as all of the inherited fields from all superclasses.

You can change the value of a local variable directly in the Variables window and then continue running your program with the new value in place.

In some cases, the debugger assigns a pound sign (#) and a number as the variable's value. This number is an unique identifier of the given instance. You can use this identifier to determine if a variable points to the same instance or to a different instance. You cannot edit this value.

Evaluating Variables in the Source Editor

You can also evaluate a variable directly in the Source Editor by moving the insertion point over the variable. If the variable is active in the current context, the value of the variable is displayed in a tool tip. In cases where a program includes different variables with the same name, the Source Editor displays the value based on the current context, and not on the instance of the variable in the source code.

You can track the changes in the value of a variable during program execution by setting a watch on the variable. When you create a watch, the value of the variable is immediately evaluated and displayed in the Watches window.

21.4.7.2 Enabling and Creating Watches in PHP Code

A watch enables you to track the changes in the value of a variable or expression during program execution. The Watches window lists all of the watches you defined for all IDE projects, of all programming languages. You can open the Watches window by choosing Window > Debugging > Watches (Alt+Shift+2).

Before you can set watches in PHP code, you must enable PHP watches in the Options window. Watches can destabilize XDebug, especially if a watch is set on invalid code.

To enable watches for PHP:

  1. Open Tools > Options (NetBeans Preferences on Mac).

  2. Click the PHP icon.

  3. Open the Debugging tab.

  4. Select Watches and Balloon Evaluation.

  5. Optionally, change the Maximum Depth of Structures and/or the Maximum Number of Children. These numbers set the visibility of nested structures and array items, respectively, during Watch evaluation.

  6. Click OK.

For more information about PHP Debugging Options, click Help at the bottom of the Debugging tab.

If watches and balloon evaluation is enabled in PHP Options, you can create watches in the Source Editor.

To create a watch from the Source Editor:

  1. Select the variable or expression in the Source Editor, right-click, and choose New Watch (Ctrl+Shift+F7).

    The New Watch dialog box opens with the variable or expression entered in the text field.

  2. Click OK.

The Watches window opens with the new watch selected.

For more details, see the following document: https://netbeans.org/kb/docs/php/debugging.html#usingAdditionalWatches

21.5 Testing PHP Applications

You can add support for PHP unit test frameworks and code analysis tools by installing plugins from the NetBeans Update Center. You can configure the global settings for the frameworks and tools in the Options window. You enable support for a test framework or tool for each project in the Project Properties window.

21.5.1 How to Configure PHP Unit Test Frameworks

You can run PHP unit tests and view test results in the IDE. You need to install the plugin for the framework in the Plugins manager of the IDE and download any necessary files. After you configure the IDE to use the framework you can configure your project to create test skeletons and run tests using the test framework.

Note:

To use a test framework you need to download the required files for the framework to your local system and specify the framework settings in the Options window. The files are not bundled with the IDE.

To configure the testing framework for a project:

  1. Confirm that the files that are required by the test framework are saved to your local system.

  2. Install the plugin for the framework in the Plugins manager.

    For more information, see Section 2.9, "Managing Plugins in the IDE.".

  3. After you install the plugin, open the Options window and click the Frameworks & Tools tab.

    For more information, see Section 2.2, "Working with the Options Window.".

  4. Select the framework in the left pane of the tab and specify the path to the files that are required by the selected framework. Click Apply and close the Options window.

  5. Right-click the project node in the Projects window and choose Properties in the popup menu.

  6. Select Testing in the Categories pane.

  7. Select a Testing Provider for the project and specify the location of the test directories for the project.

  8. Select the testing framework in the left pane of the Project Properties window under the Testing node and configure the test settings in the right pane. Click OK.

21.5.2 How to Run PHP Tests

After you write a unit test for your source code you can run tests and view the results in the IDE. You can run tests on the entire project or run tests for individual files. You can also organize tests into suites if you want to run a specific group of tests.

To run a unit test:

  1. Create a test for your source code.

    You can create a skeleton test file for a file by right-clicking the file in the Projects window and choosing Tools > Create Tests in the popup menu.

    If the test directory for the project is not specified you will be prompted to specify a test directory. If the test framework is not specified you will be prompted to select a test framework and specify the path to any required files.

  2. Right-click the project node and choose Test to run all the tests in the project.

    An overview of the results of the test are displayed in the Test Results window. You can view more detailed information about the tests in the Output window.

21.5.3 How to Analyze PHP Code

You can use code analysis tools to inspect your PHP code to compare your code to various PHP code standards. After you configure the global tool settings in the Options window you can use the IDE to inspect files and projects for potential problems in the source code.

Note:

To use a code analysis tool you need to download the required files to your local system and specify the tool settings in the Options window. The tools are not bundled with the IDE.

To analyze PHP code with an analysis tool:

  1. In the Options window, click the Code Analysis tab in the PHP category.

  2. Select a code analysis tool in the left pane of the tab.

  3. Specify the location of the script for the code analysis tool.

    You can click Browse or Search to locate the script on your local file system.

  4. Specify any default global settings for the analysis tool.

  5. Click Apply to apply the settings. Click OK to close the Options window.

  6. Open the file that you want to analyze in the editor.

    When you open one of the project files in the editor you will also have the option to analyze the entire project.

  7. Choose Source > Inspect in the main menu to open the Inspect dialog box.

  8. Select a Scope in the drop-down list

    You can select the current file, the project containing the current file or all open PHP projects.

  9. Select a code analysis tool in the Configuration drop-down list.

  10. Click Inspect to run the analysis.

    When you click Inspect the results of the analysis are displayed in the Inspector window. In the Inspector window you can navigate the tree to view the potential problems that are identified by the analysis tool.

21.6 Using a Continuous Build Server With PHP

The IDE supports creating and starting build jobs using the Hudson build server. Hudson's usefulness for PHP projects is that it allows PHPUnit tests to be performed automatically according to a schedule.

For more information about setting up and using a Hudson build server, see the following documents:

The Jenkins build server should also work with the IDE, though this is not so thoroughly tested.

Hudson is usually run on a remote machine. This section contains information for the administrator of the Hudson or Jenkins host machine.

In order for Hudson (or Jenkins) to be used with PHP and to provide test results, the Hudson or Jenkins server should be set up with the plugins described on the web page. (Hudson has equivalent plugins.) The host machine for the Hudson or Jenkins server should also have PHP and PEAR installed with the PHP plugins listed on the Template for Jenkins Jobs for PHP page.

To add a Hudson instance:

  1. Right-click the Hudson Builders node in the Services window and choose Add Hudson Instance.

  2. Type the Name for the instance that will be displayed under the Hudson Builders node.

  3. Specify the server URL, the auto-refresh setting. Click Add.

In most cases the continuous integration server is run on a remote machine. The administrator of the remote machine provides you with the URL.

After you add the server instance, a node for the instance is added below the Hudson Builders node. You can expand the node to view the status of builds on that instance.

For a PHP project to build on Hudson, the project must have the following set up:

  • Version control. The project must be under version control, and the type of version control (Git, Subversion, Mercurial, or CVS) must be supported by the continuous integration server, meaning that server must have the support plugins installed for that version control system. Check with your server administrator.

  • PHPUnit tests. The project must have PHPUnit tests defined. See the page on PHPUnit Test and Selenium at https://netbeans.org/kb/docs/php/phpunit.html.

For more details about required plugins, tools and templates that you might need to install, see:

http://jenkins-php.org/.

To set PHP options for Hudson:

  • Open Tools > Options > PHP (NetBeans Preferences > PHP on Mac) and go to the Hudson tab.

After PHP options are set you can proceed with setting up a new build job.

To set up a new build job:

  1. Choose Team > Create Build Job from the main menu. Alternatively, in the Services window, right-click the Hudson instance you want to use and choose New Build.

  2. Select the build server instance from the drop-down list.

  3. Specify the name for the build job.

  4. Select the project from the drop-down list.

    The build server will use the sources in the project's repository.

  5. Click Create. The IDE generates build.xml and phpunit.xml.dist files in the project.

  6. Commit the project's new build.xml and phpunit.xml.dist files in your version control system.

After you supply the details for the build, you can start the build process on the server by right-clicking the build that you want to start and choosing Start Job. When a job is building, the node for the job is displayed as running. You can expand the node for the job to view past builds and build artifacts.

To create builds and start jobs you must have access to a Hudson server. For more information on Hudson, see the Hudson wiki at http://www.eclipse.org/hudson/.