Sunday, September 13, 2009

PHP and Eclipse

This post is because I'm starting to program using PHP and I found no easy documentation explaining how to integrate PHP and Eclipse. I tried PDT (hated it) and Aptana and none of them actually solved my problem that was: an IDE with text highlight and a debugger. Aptana is rather confusing (I couldn't figure it out how to start a server and connect my php to it) and PDT is kind of messy.

The best I could find was a reference at Drupal documentation: http://drupal.org/node/75242#PHPeclipse. The link is http://www.phpeclipse.com/wiki/Howto/XDebugAndPHPEclipse. This was an almost complete documentation and helped me to get the job done.

First of all, the PHPEclipse editor is the best. It actually highlights PHP files, even though they do not possess a ".php" extension. (The other editors just cann't tackle this fairly easy task.) And it is really easy to debug your code (there is a catch nonetheless) and start playing around. For this documentation I will not repeat things that are already documented, so you will need to have the documentation below opened:

http://www.phpeclipse.com/wiki/Howto/XDebugAndPHPEclipse

1. Get Eclipse Galileo (the version that I have and in which this documentation is based on) at:

http://www.eclipse.org/downloads/

2. Install Subclipse plugin (for SVN):

Eclipse update site:
http://subclipse.tigris.org/update_1.6.x

3. Install PHPEclipse plugin:

Eclipse update site:

http://update.phpeclipse.net/update/stable/1.2.x

3.1. It is VERY important that you disable the DBG plugin. See the "Special Note:" at http://www.phpeclipse.com/wiki/Howto/XDebugAndPHPEclipse

4. Install XDebug (not in Eclipse):

4.1. Get some additional packages:

#---
yum -y install \
php-devel
#---


4.2. Get XDebug source, configure, compile and install it:

#---
cd /tmp
wget http://www.xdebug.org/files/xdebug-2.0.5.tgz
tar -vxzf xdebug-2.0.5.tgz
cd xdebug-2.0.5
phpize
./configure --enable-xdebug
make
mkdir -p /opt/xdebug-2.0.5
cp /tmp/xdebug-2.0.5/modules/xdebug.so /opt/xdebug-2.0.5/
#---


Note: It will be installed at /opt/xdebug-2.0.5/

4.2. Configure the XDebug for the PHP:

#---
cat >> /etc/php.d/xdebug.ini << __END__
;
; Configuration taken from: http://www.phpeclipse.com/wiki/Howto/XDebugAndPHPEclipse
;
[xdebug]
; for non threaded php (debug and inside eclipse)
zend_extension="/opt/xdebug-2.0.5/xdebug.so"
; for threaded php (apache)
; zend_extension_ts="/opt/xdebug-2.0.5/xdebug.so"
xdebug.remote_enable=On
xdebug.remote_autostart=On
xdebug.remote_handler=dbgp
; change it if you want to debug from another server/workstation
xdebug.remote_host=127.0.0.1/32 192.168.0.0/16
xdebug.remote_port=9000
xdebug.remote_mode=req
__END__
#---


4.3. Configure your Apache to have an Alias to your workspace, but remember: you HAVE to give permission to the apache user to reach you workspace:

#---
chmod o+r ${HOME}
chmod -R o+r ${HOME}/<your workspace>
echo "Alias /dev \"${HOME}/<your workspace>/<your php project name>\"" > /etc/httpd/conf.d/php-dev.conf
service httpd restart
#---


5. Configure Eclipse to use XDebug:

Follow http://www.phpeclipse.com/wiki/Howto/XDebugAndPHPEclipse from "Test Project" section to the end.

Note: The URL for your project will be: http://localhost/dev/<your PHP file in your workspace>

Note2: When you launch a debug (in Eclipse) you have to switch to the Debug Perspective manually (that is the catch that I mentioned).

Note3: At the "pathmap" configuration (for the PHP XDebug Remote Script) you will use: /home/<your username>/<your workspace>/<your php project name> in both fields (this is due to the fact that you are running the http server locally and using an Alias).