Monday, March 14, 2011

PHP PEAR libraries and Eclipse

If you develop PHP applications using Eclipse, here is a tip that may come in handy.  PDT, the Eclipse plugin for PHP development, will, among other things, display the documentation for any method, function, or field that you hover your mouse over.  And you can jump to the definition of that method, function, or field by control-clicking on it.

But if your application uses libraries installed with PEAR, then this extra functionality will not work for those libraries.  However, this can be fixed by providing Eclipse with the locations of these libraries.

Note: Click on the screenshots below to see a larger image.

1. Right click on your PHP project and select "Properties".

2. Select "PHP Include Path" from the left-hand menu.  Click the "Libraries" tab, then click "Add Library...".

3. Select "User Library" and click "Next".

4. Click "Configure...".  Another new dialog will appear.

5. You want to add a new library, so click "New..." and type in a name for the library.  I'm going to add PHPUnit.

6. Select the newly added Library and click "Add External folder...".  This is where we point to the file system location of the library.

7. Select the path to the library.  In Windows, PEAR installs its libraries inside the "PEAR" folder under the PHP installation directory.  On Mac OSX, its location is "/usr/local/PEAR".  On Ubuntu, it should be at "/usr/share/php/PEAR".  If you can't figure out where your PEAR directory is, you can find it in the "include_path" setting in your php.ini file (when PEAR is installed, it automatically modifies the PHP include path to include itself).  Click "OK".


8. Click "OK", then click "Finish"!



In my next post, I'll explain how to run PHPUnit unit tests from Eclipse without needing any plugins!

Do you have any tips for developing in PHP on Eclipse?  Let me know in the comments section!

7 comments:

Anonymous said...

Thank you, Mike !
your post was extremely helpful.
On Ubuntu PEAR usually located in
/usr/share/php/PEAR
Ivan

Michael Angstadt said...

Thanks for the tidbit, Ivan!

Michael Angstadt said...

Thanks! ^^

Anonymous said...

Hi Mike,

I can't open php code declaration. For example I have code like this
$sql = mysql_query($q);

I can't open mysql_query to see that declaration.

Do you know how to fix it on Eclipse?
I use Eclipse Helios and I have installed PDT Plugin to work with PHP

Michael Angstadt said...

Anonymous, built-in functions like mysql_query are not actually written in PHP. I believe they are written in C and then compiled into the PHP language. To see the source code of these methods, you'd have to download the source code of the PHP language.

joomkb said...

Thanks for giving great information for me. This is very useful for me. because i am a beginner for php.

Isa Guha said...

Good to see your blog.