Tiger Heron
Tiger Heron
Tiger Heron

Eclipse and PHP

Average: 4.5 (15 votes)

In my last article, I talked about installing PHP. With PHP installed, you will want to start writing code, so today's article is about developing PHP using the open source Eclipse integrated development environment (IDE).

Eclipse started as a Java IDE and evolved into a generic IDE with a strong Java bias. If you start out with a standard Eclipse installation and without any Eclipse experience, it may take you a while to figure out the best way to set it up for use with PHP. This article will make the PHP setup fast and easy.

Why use Eclipse?

The reasons for using Eclipse are the same as those for using most IDEs:

  • Syntax highlighting
  • Syntax validation as you type
  • Editing assistance (indentation, auto-completion, etc.)
  • Access to documentation as you type
  • Refactoring assistance
  • Debugging assistance
  • Integration with other tools (documentation creation, source control, etc.)

Currently, the PHP version of a feature is often more limited than the Java version. For example, refactoring is limited to moving files; if you want to rename a class, method or function, you will need to use the Find/Replace tool (which, luckily, can be applied to all files in a project). I expect that the PHP tools will improve with time.

PDT

Eclipse consists of a core set of functions and a flexible plugin system. The plugins provide many of the features that we usually think of as standard parts of an IDE. This is what makes it possible to adapt Eclipse to many computer languages.

PDT is the PHP Development Tools plugin, which is essential for working with PHP inside Eclipse. Over time, you may choose to enhance your Eclipse installation with other plugins, but for PHP development, you only need PDT (and its few dependencies).

Getting started: Download

Begin by downloading the all-in-one Eclipse/PDT ZIP file. This contains all you need to start using Eclipse with PDT.

If you don't have Java Runtime Environment (JRE) 5 (or greater) installed, then you'll also need to download the latest JRE.

Getting started: Install

Start by installing the JRE, if you need it.

The Eclipse all-in-one package comes as a ZIP file. On Windows, you can extract the contents to C:\Program Files. This will create a folder called "eclipse". For easy access, copy the eclipse.exe file in the folder and paste it as a shortcut on the desktop.

Getting started: Configure

Start Eclipse by running the eclipse.exe file. This will ask you to select a "workspace", a folder that will store a collection of your projects. You can have more than one workspace.

Eclipse will suggest a location for the workspace. If you're happy with it, click OK. On Windows, I would suggest using something like My Documents\Eclipse. If you don't intend to create multiple workspaces, select "Use this as the default and do not ask again" before clicking on OK (you can change your mind later).

Eclipse should appear and display the Welcome screen (you can return to the Welcome screen through the Help menu). Now I will introduce you to Eclipse's rather intimidating preferences system: select Window > Preferences. Under PHP > PHP Interpreter, select PHP 4 or 5.

There are many other options you will want to customize, but this is sufficient to get started. You'll return to this menu many times to tweak how Eclipse works.

Getting started: Update

You should also become familiar with Eclipse's update system. Select Help > Software Updates > Find and Install > Search for updates of the currently installed features > Finish. Select a mirror or let Eclipse select one for you. Eclipse will now begin a rather slow search for updates. You can place the update dialog into the background and continue to work while the search takes place.

When Eclipse locates all the updates, a window will appear, showing you your update options. Select everything and begin the update. Once you have installed all updates, you may need to restart Eclipse. Your installation is now up-to-date.

The update method is also the way you add new features to Eclipse. Use Help > Software Updates > Find and Install, but this time select "Search for new features to install". You'll be able to search for features on a collection of sites.

For example, let's say you use doxygen to generate your PHP documentation. You would click on "New Remote Site", enter "Eclox" for the name and http://download.gna.org/eclox/update/ for the URL. This creates a new entry in the sites list.

You can visit Eclipse Plugin Central to find additional plugins to install. But let's leave that for later.

Creating a project for existing PHP code

Your Eclipse installation may coincide with the creation of a new PHP project, but it is more likely that you already have existing code you want to use within Eclipse. This is where most people get a little stuck—if you know the magic incantation, the process is easy.

Let's say you have a PHP Web site at My Documents\Sample\Web Site and that you test the Web site using the local server address http://localhost/sample.

Fire up Eclipse and select File > New > PHP Project. Enter a project name. Disable the Project contents > Use default toggle and browse to the My Documents\Sample\Web Site folder. Click Next. The files in the My Documetns\Sample\Web Site folder are now part of an Eclipse project.

You probably also reference code through a PHP include path. Select Libraries > Add External Folder to include folders in your Web site's include path. Click Finish.

Our project now appears in Eclipse's PHP Explorer pane. Eclipse will place a .project file in the My Documents\Sample\Web Site folder, but your content is otherwise undisturbed. You are ready to use Eclipse. You can also continue to work on your code using your existing development tools.

Editing with Eclipse

Using the PHP Explorer pane, you can double-click on a file and have it appear in Eclipse's editor pane. The features here are pretty standard, so I won't talk about them too much. You may want to return to the preferences dialog to customize the editing features.

One nice editor feature is called "code assist". Eclipse will pop up various displays that offer documentation or auto-completion help. If the code assist popup doesn't appear automatically, you can call it with Ctrl + Space. One caveat: if you have enabled the Emacs key bindings, the magic sequence is Alt + /.

Executing PHP

You can still run your Web pages by typing http://localhost/sample/some_page.php into your Web browser. But let's try running the page from eclipse.

First, some setup: Using Window > Preferences > PHP > PHP servers, we create a new server, Sample, with a URL of http://localhost/sample. If you would like to use your default browser, go to General > Web Browser, select Use external browser and enable Default system Web browser.

Open up your Web page in the editor. Select Run > Run As > PHP Web Page. In the dialog that appears, verify that the Launch URL is correct. The page will appear in your Web browser.

You may notice that the URL has many parameters attached to it. These are for coordinating with the debugger. This may be a bug in Eclipse since debug parameters should only be added when debugging a script.

Go to Run > Open Run Dialog. You'll see that you can create a "run configuration" that controls what happens when you select Run. For my "sample" project, I right-clicked on PHP Web Page and selected New. I set the debugger to XDebug, the PHP Server to Sample and the file to /Sample/index.php. On the Common tab, I toggled Run under Display in favorites menu.

Now I can run the index.php page by selecting the Run icon on the tool bar and then selecting the run configuration I created from the tool bar drop down. Using this method, the page appears without the debugging parameters attached.

As far as I can tell, you would have to create one run configuration per page. Or, as I pointed out earlier, you could just enter the page address directly in your browser and bypass Eclipse.

Debugging PHP

If you have your system set up to use either the Zend debugger or XDebug, it's easy to debug with Eclipse.

Configuring PHP for debugging is beyond the scope of this article. The Eclipse portion of the setup involves using Run > Open Debug Dialog to get to a dialog very similar to the Run Dialog. Create a configuration for debugging index.php and enabled the Debug toggle on the Common tab.

On the tool bar, you can use the debug button drop-down to select your debug configuration. Normally, the debugger will break on the first line.

When you start debugging, Eclipse will offer to switch to the PHP Debug perspective, which will give you the right panes and controls to run the debugger.

Additional resources

I hope these instructions give you a head start at using Eclipse for PHP. Here are some other places you may get assistance.


As addition to pdt a very

As addition to pdt a very nice ssh, sftp console tool

http://www.eclipse.org/dsdp/tm/tutorial/
update url: http://download.eclipse.org/dsdp/tm/updates/2.0/

Above needs the following library: (install this first)
http://www.eclipse.org/cdt/downloads.php
update url: http://download.eclipse.org/tools/cdt/releases/europa

to update/install these kind of tools:
Help -> Software update -> find and install -> search new features to install -> new remote site

Enjoy your new perspective (top right) remote system explorer


Thanks!

Thanks for the article, it got me started with Eclipse and PDT.


good article - but can you 'share' some info. on other IDEs?

For example, Netbean and its 'new' php support :)
Thanks!
J


Hi, Jack, I've used Netbeans

Hi, Jack,

I've used Netbeans in the past, but switched to Eclipse because it seemed most other people were using it. I'd love to share some info on Netbean's PHP support, but I have nothing to share. Perhaps you could write an article and drop a pointer to it here?

Tony Freixas
Tiger Heron LLC
http://www.tigerheron.com


what server?

Hi Tony, I really appreciate you putting up this how-to guide, but I'm still too dumb to figure it out. I'm a n00b at php, but I downloaded PDT and Eclipse and Zend and have run my php as a script successfully. But everytime I try to run it as a Web Page, I get the host not found. This is understandable (as a Java developer) because I've not associated my project with any server; I've not even created a server! I tried to create an HTTP Server instance, but that did not work.

I do see my Default PHP Server that was created as http://localhost, but how does it run? How do I associate it with my project?

thanks,
josh


Hi, Josh,

You'll need to install a server on your PC. I use Apache 2. There are plenty of sites out there with instructions on setting up Apache to run PHP. Then you need to put your PHP code where the server can find it. If you use http://localhost, then the code needs to be wherever you set up your server's document root.

I'm not sure if my answer makes sense to you, but try searching for information on Apache and PHP and what you find may help this start making sense.

Tony Freixas


Thanks!

Thanks for your quick response. I guess I was over thinking it. I have a LAMP server already running on my Linux box, but none of the tutorials for Eclipse spell out that you first need to create the http server and get PHP running before you can make use of testing with Zend and PDT. I think I was or am also confused about what Zend does. Like I said, I'm still a n00b.

I'll give it another whirl. thanks, again!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

 

Privacy policy Tiger Heron LLCinfo@tigerheron.com • (503) 771-7724

Copyright © 2005-2007, Tiger Heron LLC