Using Eclipse to Debug PHP

Windows Development 1 Comment

With EasyEclipse and the PHP Debugger extension, you can debug and step through a PHP call.

Installing the PHP Debugger (DBG) Extension

  1. Download the PHP Debugger (DBG); specifically the DBG 2.15.5 dbg modules for windows.
  2. Unzip the dbg modules to a temporary directory.
  3. Copy the relevant php extension file, currently “x86\php_dbg.dll-5.2.x” in the archive (since XAMPP 1.6 comes with PHP 5.2.5), to the extension directory “c:\xampp\php\ext” and rename it to “php_dbg.dll”.
  4. Open up the “c:\xampp\apache\bin\php.ini” config file.
    • Add this to the end of php.ini:
      debugger.ports=7869, 10000/16
    • Search for the first instance of “zend_extension_ts” in php.ini (it should be right underneath the “[Zend]” section) and before it, add this line:
      zend_extension_ts = "c:\xampp\php\ext\php_dbg.dll"
  5. Verify that the DBG Extension is loaded:
    • Stop and start the apache http server (a reload or restart won’t work).
    • Create a phpinfo.php file with this content:
      <title>PHP Test Script</title>
    • Install phpinfo.php under the apache root document directory and then browse to it. You will see the info on the PHP environment. If DBG is installed correctly, you should see this line:
      Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
          with DBG v2.15.5, (C) 2000,2007, by Dmitri Dmitrienko
    • Search on “dbg” and you will see the configuration section for dbg also.

Disabling PHP Debugging

  • Once you have installed the DBG extension, debugging will be enabled and for any PHP file that is browsed to, the PHP engine will attempt to debug and fail because we have not setup a PHP Debugger service yet.
  • To disable debugging permanently:
    1. Open up the “c:\xampp\apache\bin\php.ini” config file.
    2. Change the “debugger.enabled=on” to “debugger.enabled=off”.
    3. Restart the Apache HTTP server.
  • To disable debugging for the current active HTTP session:
    1. Browse to “http://localhost/myproject/index.php?DBGSESSID=-1”.
    2. This URL will remove the cookie added by the DBG extension to enable PHP debugging.

Starting the PHP Debugger Service

In order for PHP debugging to work, we must configure and start a PHP Debugger service using EasyEclipse:

  1. Run EasyEclipse and go to menu “Run->Debug…”. If you don’t see the Run->Debug menu, then open up the Debug perspective, switch back to PHP perspective, and try again.
  2. Select and highlight the “PHP DBG Script” and click on the New icon on the upper-left.
  3. Input whatever Name you want.
  4. Under the File tab, input “index.php” (or your main entrypoint PHP file). (Alternatively, you can Browse… and select it.)
  5. Under the Arguments tab, you should see “Use default working directory” checked and the “Working Directory” is “C:\Projects”.
  6. Under Environment tab:
    • Under the Interpreter sub-tab, click the New button and browse to “c:\xampp\php\php.exe”
    • Under the “Remote Debug” sub-tab, check “Remote Debug” and uncheck “Open with DBGSession URL in internal Browser”. Make sure that the Remote Sourcepath is “C:\Projects\myproject”.
    • Note: It is very important that the Remote Sourcepath (and all other paths) is set correctly; otherwise the PHP debug won’t work.
  7. Hit the Apply button.
  8. Start the PHP Debugger service by clicking on the Debug button below the Apply button. Take note of the port number (should be 10001) that the PHP Debugger service is listening on.

Debug Tracing PHP

  1. Open up “index.php” and set a breakpoint inside it (I suggest setting a breakpoint in the main entry routine).
    • You can set a breakpoint by clicking on the leftmost vertical blue bar in the file viewer. You should see a blue dot show up and an entry added to the Debug perspective’s Breakpoints window.
    • Alternatively, you can right-click on the leftmost vertical blue bar and select “Toggle PHP Breakpoint”.
  2. Browse to “http://localhost/myproject/index.php?DBGSESSID=1@localhost:10001” and the breakpoint should be hit. You can then step into, over, or out. Yippee!
    • Note: The 10001 in “localhost:10001” should match the port that the PHP Debugger is listening on in the EasyEclipse debug window.
    • Passing in the request parameter DBGSESSID will create a cookie (session based), so you no longer have to pass in the DBGSESSID http parameter in subsequent requests. Yipee again!
  3. Disable debug tracing for the current session by browsing to “http://localhost/myproject/index.php?DBGSESSID=-1”. Adding the parameter “DBGSESSID” with value “-1” will delete the debugging cookie.
  4. CAVEAT: Unfortunately, there is a bug with the PHP Debugger service in that after servicing the HTTP request, the PHP Debugger service will terminate.
    • You can quickly start the PHP Debugger service again by hitting F11 shortcut key. You will want to do this after each HTTP request is debugged.

Note: Some info above was derived from XOOPS Docman – Installing DBG.

1 Comment

Using Eclipse to Develop in PHP

Windows Development No Comments

easyeclipseThe EasyEclipse distribution is a quick and easy packaging of Eclipse with plugins for different purposes. I recommend using EasyEclipse for PHP which comes with PHP edit mode and useful plugins like SubEclipse for subversion source control integration.

  1. Download and install the latest version of EasyEclipse for PHP.
    • When you launch EasyEclipse, it will prompt you for the workspace directory. Input “c:\projects”. If EasyEclipse doesn’t prompt you for a workspace and instead uses a workspace from an old project, you can manually change the workspace by going to menu “File->Switch workspace”.
    • Note: It is not recommended for you to move or rename your workspace directory once created. The workspace properties have a reference to the original workspace directory.
  2. At this time, checkout the myproject codeline to c:\projects.
    • Go to the EasyEclipse menu “File->New->Project->SVN->Checkout Projects from SVN”.
    • Enter the Repository URL: http://localhost/repos/
    • Then select directory: /myproject
    • Change the project name to: myproject
  3. Edit PHPEclipse options to run XAMPP server:
    • Go to the php external tools settings “Windows->Preferences->PHPeclipse Web Development”. (The following steps will be referenced off of this location.)
    • Set the xampp start and stop buttons under “PHP External Tools->XAMPP”.
    • Set the DocumentRoot path under “Project Defaults” to: C:\projects
    • Add the following to the “Include Paths” under “Project Defaults” to: C:\projects\myproject
    • (Optional) Turn off php file browser preview. Under “Browser Preview Defaults”, uncheck “Refresh PHP browser view…” and “Show PHP browser view…” selections.
  4. (Optional) Configure the tab width to be 3:
    • Go to “Windows->Preferences->PHPeclipse Web Development->PHP”
    • Set “Displayed tab width” to 3.
  5. (Future) Synchronize your source to the latest SVN repository:
    • In the left-hand Navigator, right-click on “myproject”.
    • Select right-click menu “Team->Update”.
  6. Hint: If you are not in PHP edit mode, you can always go to it by going to menu “Windows->Open Perspective->PHP”

Note: If the subversion commands you issue using SubEclipse return errors, you may have an incompatible SVN install directory in the “path” environmental variable. To resolve this, you can remove the SVN install directory from the “path” environmental variable or you can re-install a compatible SVN version. For example, SVN 1.4.6 is compatible with SubEclipse 1.2.1 which comes with EasyEclipse 1.2.2.

No Comments