Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Profile photo of support-octavio
Octavio Gonzalez
Web Developer spending each day wrangling front and back-end code - helping keep Genuitec on the web.

Introduction

If you code PHP and are tired of fighting logging messages, Eclipse PHP Development Tools (PDT) can end the fight! Using Eclipse PDT to debug PHP isn’t as hard as you might think. Read on to see how a few simple steps can make your coding life so much easier!
image00E

Background

Our portal uses WordPress, a powerful content management system (CMS), and we have written many custom plugins with specific requirements for our company. In the beginning, a debugger wasn’t necessary. But, as functionality got complex, we needed to find an easy way to debug PHP. This search lead us to a real time saver—Eclipse PDT along with Xdebug. Now we just use breakpoints and watchers to debug instead of the tedious task of printing out data and debugging the error log file.

Example

The following example demonstrates how to set up a PHP debugger for WordPress in just a couple of steps—making debugging PHP as simple as debugging JavaScript in your favorite browser or even better, in Webclipse.  We’ve also included information about profiling your app to help find bottlenecks.

Requirements

  • Apache server
  • PHP
  • Eclipse Luna

Setup

Before you start debugging, you will need to install Xdebug and configure your project in Eclipse.

Xdebug

To install XDebug in an Ubuntu-based distribution, run the following command in your CLI:
sudo apt-get install php5-xdebug

This installs the xdebug extension in the default php extensions directory, setting it enabled by default.

In Windows, I like to use Xampp. After installing it, Xdebug is pre-configured in the php.ini file.  To enable Xdebug, remove the semicolon ( ; ) to uncomment the line that manages the setup and settings.

Eclipse

Complete the following steps to configure a PHP project in Eclipse for debugging.
  1. Create a new project using the location of the current WordPress location.
  2. Click the Debug drop down arrow and select Debug Configurations.
    image08
  3. Select PHP Web Application and click New_configuration. The following window displays:
    image02
  4. On the Server tab click Configure to edit the default server or New to create a new server.  Enter the following information:
    Name—Type a name for the server.
    Debugger—Select XDebug.
    Base URL—Type the URL for your local domain test.
    Local Web Root—Set the path of your web root.

    Here are what my settings look like:
    image11
  5. Click OK and return to the Server tab on the Debug Configurations window.
  6. Select the PHP file to debug (the file from which the program starts).
  7. Configure the URL:
    image04Note: I configured my virtual domain ( ‘portal’ ) to load content in C:\Users\Octavio\git\wordpress, so I had to disable Auto Generate and remove ‘wordpress’ from the path.
  8. Click the Debugger tab and make sure XDebug is selected as the Server Debugger. Optionally, you can set a breakpoint at the first line of code.
    image10
  9. Click Apply to save your changes.

Debug

Now you have configured XDebug. To get started debugging, open the debug configuration profile you just set up and click Debug. The first time you run the debugger, you get an alert to confirm switching to the Debug Perspective. If you checked the Break at First Line option, your app suspends at the first instruction in the index.php file:
images00E1

Click the Stop button if you want to stop debugging:
image01

Now, follow these steps to debug something basic but useful.

  1. In a fresh installation of WordPress add the following code to the functions.php file of current theme:
  2. Set a breakpoint on the first line of the function and a second breakpoint on the last line (return $content;).
  3. Click Debug and notice that execution stops at the first breakpoint. Also note that we have access to the program variables, in this case the important one is $content.
    image05E
  4. Click Resume or press F8 to continue the program execution.  The program execution now stops on the second breakpoint.image06
    Notice the $content var has changed due to our function’s logic. In some cases, there are too many variables for the Variables tab to be helpful.  In these situations, use the Expressions tab and add only the variables you want to inspect:
    image03E

Bottlenecks

Sometimes we find a bottleneck in our application and don’t know which function is causing it, that’s when adding a profiler to our scripts comes in handy. Xdebug provides great data for profiling PHP and is easy to use.

Add a profile to a script

  1. Add the following to the php.ini file:

    Details
    xdebug.profiler_output_dir—The path where the profiles are saved.
    xdebug.profiler_append—If On, adds the data at the end of the file instead of replacing last profile.
    xdebug.profiler_output_name—Specifies the profiler’s name
    xdebug.trace_options—If 1, appends the traces instead of overwriting.
    xdebug.collect_params—Specifies if we want to consider the parameters in functions. We use 4 for a full variable contents and variable name. See more options
    xdebug.collect_return—If 1, XDebug writes the returning value of functions calls to the trace files.
    xdebug.profiler_enable—If On, enables profiler.

  2. Make sure Apache has read and write permissions to the profile’s directory.
  3. Webgrind is a web-app that shows the data saved with Xdebug. Download Webgrind and install under /
  4. Modify the config.php file with our profiler directory:
    $profilerDir ="/home/admin/traces/";
  5. Open Webgrind in a browser:
    image07

Conclusion

I hope you are able to put this information to good use.  Debugging this way has certainly saved us a lot of time (and also headaches!).  It may seem confusing at first, but once you have it configured properly you will find the time spent setting up was totally worth it.  

DEBUG-blog


Related Posts

Equifax Data Breach Advisory Many of you may have heard of the Equifax data breach, possibly affecting millions of consumers. Information accessed by hackers during the incident included Social Security numbers, personal details, driver’s license numbers, and even credit card numbers.Equifax has confirmed that the breach was made possible by a vulnerability in the Apache Strut...
Building Applications with Angular Material Angular Material is a set of high-quality UI components developed by the Angular team, based on the Google Material design specification. These components help us to build applications with an attractive, yet familiar UI, giving users a consistent experience across devices.In this Angular tutorial, you will learn how to set up material design in yo...
Realtime Server Monitoring App with Angular 4 , No... This article will show you how to build a very simple monitor that allows you to observe some OS parameters, such as free memory available. We will be using Node.js, Angular 4, and Chart.js - and all you need is Angular IDE.We will be creating two applications; the first is a Node application that will monitor the OS parameters and send them via we...
Beware of Software Scams Even though imitation might be the best form of flattery, we are Genuitec, LLC, not Genuitec, Inc. Someone is using our name and old logo to run a number of scams, including real estate transactions and software sales, with the clear intent to hide behind Genuitec’s good name. We are, however, already taking action to demand an immediate cease and ...

Posted on Oct 1st 2015