Debugging a Php project with NetBeans

Getting stuck while coding and testing right away happens to any programmers, sometimes looking at the logs is not enough to fix things quickly, that’s when using a debugger becomes really helpful.

We are going to integrate XDebug inside our NetBeans configuration so we can debug Php 7 projects.

Following is the configuration I will be working on :

  • Remote Web Server using Php7 and Apache 2
  • A Zend 3 project
  • Local machine using Netbeans 8.2
  • The SFTP settings are correctly set up to upload files to the server on save

If you need to install NetBeans, then follow the steps described here : Netbeans installation.

The local machine is running on Windows but that does not impact the procedure if you are on Linux or Mac as the server is on a remote machine.

Get started with XDebug

Let’s first install the debugger :

# apt-get install php-xdebug

Then enable the extension :

# phpenmod xdebug

Now check the file under /etc/php/7.0/mods-available/xdebug.ini and the following lines into it :


zend_extension=xdebug.so
xdebug.idekey=netbeans-xdebug
xdebug.remote_enable= On
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_host=888.888.888.888
xdebug.remote_port=9000

Replace 888.888.888.888 by your local IP (the IP where you are running NetBeans IDE).

Restart your Php server and you should see this :

Composer Serve - XDebug

See the warning message ? Of course it does impact the performance of your website so never achieve this on a production server, you better use it using a virtual machine or on a local server.

Verify your php configuration using the following command :

# php -i | grep xdebug

On your local machine running NetBeans you should allow the 9000 port and if you have a router you have to redirect the port to the local machine or Netbeans won’t be able to connect to the server.

Using a remote server requires mapping the paths on the NetBeans Advanced Web Configuration :

  • Right click on your project name on the left and click on properties

Advanced Configuration NetBeans

Mapping Paths Netbeans

Try to match your local and remote paths and click OK.

If you launch the debugger from NetBeans now (CTRL-F5), that should be working, we will see now how we can test it with the default Zend 3 application.

XDebug test

For precise details about the Debugging tools on IDE, check the following url.

If you only want to do a rapid test, then set up one or more breakpoints within your project files, for instance in the indexController.php of the Zend 3 application at line 17.

  1. Launch the Debugger
  2. The page is stopping at the breakpoint, see the following screenshot :

Breakpoints - Xdebug

Browse each console tabs to become familiar and make further testing by adding more breakpoints !

At any time if you need to disable Php debugging then issue the command :

# phpdismod xdebug

Well we are all good now, in a next tutorial we will review the use of PhpUnit along with NetBeans and XDebug tool, we will also discuss about KCacheGrind for profiling our Php code.

Don’t forget to follow us on Social media for up to date informations.

 

Leave a Reply

Want more information?

Related links will be displayed here in this section for you to pick up another good spot to get more details about Web marketing and Search Engine Optimization. There will be some sites which we selected to ease the work of any webmaster or/and web marketer on the Internet.

%d bloggers like this: