How to Configure PHP Project in Eclipse for Debug Mode

Eclipse is very important tool for any developer, all due to the vast library of open source and freely available plug-ins which provides almost every required facility for development environment.

Now if we are using Eclipse, then ‘debug’ mode in Eclipse is a very useful tool to debug the code. Putting prints in the code and tracking the print-outs on the server console is very tedious job. So here we are explaining; how we can configure Eclipse and PHP web server to enable debugging of PHP applications in Eclipse using XDebug (http://www.xdebug.org/docs/).

As PHP code runs on web server, so that means, debugging can be performed only on server. So we put the debugging information in PHP code file using eclipse in IDE, however we need to install XDebug on web server for actual debugging. XDebug will sit on the server, and will generate the required debugging information like variable dump, stack traces etc and put all of this information for public use on a specific port of the server. Now any application can access this debug information by sending request to server on this port.

Eclipse works on same concept for showing debugging information for PHP application. It connects to the web server on given port, and fetches the debug information to show in a user friendly way with its UI. So to configure the debug mode in Eclipse, we need to configure XDebug on web server and also need to configure the eclipse for XDebug properties. Configuration steps are as following:



Configure XDebug on server (for Window):

  1. Install web server (or XAMPP) on our system.
  2. Ensure that php>ext directory has the xdebug library. Please ensure the latest and compatible version for your current PHP installation and the OS.
  3. You can download the right version of XDebug from http://www.xdebug.org/download.php
  4. Check which php.ini (path) is in use by server by print phpinfo()
  5. Open php.ini and make the following changes
    1. Find ‘implicit_flush’ and set it to ‘on’
    2. Find line with zend_extension attribute and uncomment it.
    3. Set the value of zend_extension to the current path of xdebug library file in php > ext folder.
    4. Attribute name was zend_extension_ts before PHP 5.3
    5. Find the section starting with [zend] and comment all the lines by putting a semi-colon ‘;’ at the start of each line.
    6.  Find the section starting with [xdebug] and uncomment all the required attributes. Mostly required attributes are:
      1. Xdebug.remote_enabled = true
      2. Xdebug.remote_host = localhost
      3. Xdebug.remote_port = 10000
      4. Xdebug.remote_handler = dbgp
      5. Xdebug.profiler_enabled = 1
      6. Xdebug.profiler_output_dir = ”C:\xampp\temp”
    7. Please ensure to set above properties as per your configuration, like remote host can be other than ‘localhost’.
  6. Now XDebug is configured on the server.
  7. Restart the web server.
  8. Print PHPInfo on browser, it can be done from control panel if you are using xampp.
  9. You will find XDebug section in PHPInfo page which will ensure that XDebug is configured on server now.
  10. Common problems
    1. Incompatible version of XDebug library
    2. Wrong php.ini


Configure XDebug in Eclipse for enabling the Eclipse Debug Mode:

  1. Install Eclipse (PDT) on your system. You can download it from http://www.eclipse.org/pdt/downloads/
  2. Create workspace on your Eclipse.
  3. The easy way to have PHP files run directly from your Eclipse is to create the workspace in ‘htdocs’ folder on server and then create and save the project contents in this workspace. This will automatically deploy the any new developed code to web server.
  4. Alternatively you can keep your source code in some other directly, and can use ant like script to deploy the final code to web server.
  5. Open Preference dialog from Window > Preferences
    1. Go to PHP > Debug node
      1. Set PHP debugger to ‘Xdebug’
      2. Ensure that ‘Break at first line’ checkbox should be checked.
    2. Go to PHP > Debug > Installed Debuggers
      1. Set the port of XDebug to ‘10000’
      2. Ensure that port for Zend should be other than ‘10000’, like it can be ‘10001’
    3. Go to PHP > Debug > Workbench Options
      1. Set ‘allow multiple debug sessions’ to ‘Never’
      2. Set ‘Switch back to PHP Perspective’ to ‘Always’
    4. Go to PHP > PHP Servers
      1. Ensure that your web server localhost address is set as Default PHP Web Server, and your workspace path should be appended to the url like http://localhost:8080/workspace
  6. Now create any new project. By default, it will be created in default workspace i.e. in web server’s htdocs folder and hence automatically will be deployed to server
  7. Create a new Test.php file
  8. Put some code to the file
  9. Put a breakpoint at first line
  10. Right click on the file and select to ‘Debug as > PHP Web Page’
  11. Eclipse should open the default system browser for the page, and the breakpoint should be triggered on first line of code.
  12. Now you can debug the application using various instructions of eclipse debug mode, like step-in or step-over etc.
  13. Common Problems
    1. XDebug option is not set in preferences
    2. Port is different than what you have set in php.ini

This is all to configure the PHP web application in Eclipse for debugging mode. Happy Debugging!!

People who read this post also read :



0 comments:

Post a Comment