PHP Programming With Eclipse IDE


COVER STORY PHP with Eclipse
PHP programming in the Eclipse IDE
CONTROLLED
ECLIPSE
The PHPEclipse plugin brings the PHP scripting language to the Eclipse
development environment, and the Subclipse plugin integrates version
control. BY ALEX LEGLER AND PETER KREUSSEL
he free and popular Eclipse de- needs direct access to the source code. install via the Eclipse plugin interface
velopment environment [1] gives When you launch Eclipse, select the [4]. Help | Software updates | Find and
Tprogrammers a feature-rich edi- server s root folder as your Workspace. install opens the update tool (Figure 1);
tor, version control manager, and debug- Also, make sure that the user running then, enable the Search for new features
ger on a shared GUI. Eclipse was de- Eclipse has write privileges. to install option and click Next before
signed as a universal IDE that is easily The configuration described in this pressing the Add remote Site button and
extensible to accommodate a variety article requires the following software: typing the URL http://subclipse.tigris.
of programming languages. The PHPE- " A web server on localhost with PHP org/update_1.0.x/. The user executing
clipse plugin converts Eclipse into a support. Eclipse must have write privileges for
full-fledged IDE for PHP-based dynamic " Subversion with a Subversion server the program directory.
websites. Another plugin called Sub- running: If your distri-
clipse integrates version control with the bution supplies the
Subversion version management system Subversion server in a
[2]. In this article, we describe how to second package, you
set up a PHP development environment will need to install this
with Subversion and Eclipse. package, too.
If you are developing dynamic web- " The Eclipse IDE with
sites, it makes sense to install a web the Subclipse and
server that supports your choice of PHPEclipse plugins.
scripting language locally. In this article, Eclipse requires the
we describe a setup in which all of the Java Runtime Environ-
required components run on the local ment version 1.4 or
machine. However, the Subversion sys- newer.
tem could run on a central server to give
Installation
access to a team of developers.
Debugging requires that you have a The Subclipse [3] plugin
project folder in the web server s root di- provides Subversion sup-
rectory. To debug, you need to execute port for the Eclipse IDE.
the files on the web server to allow the Subclipse, which is avail-
server to run the embedded PHP code. able under the Common Figure 1: The Eclipse plugin interface can help you install
On the other hand, the debugger also Public License, is easy to Subversion and many other extensions.
ISSUE 87 FEBRUARY 2008
40
40
lino beltrame, Fotolia
PHP with Eclipse COVER STORY
Once you have completed an editing
Listing 1: Server Configuration
step, you can commit the changes to the
01 ### conf/svnserve.conf
repository: To do so, right-click the file
02 [general]
or folder and select Team | Commit
(Figure 2). A comment can be added
03 password-db= passwd
to each version created by a commit,
04 realm = example realm ### a kind of "namespace"
and it makes sense to use this option
05 anon-access = none ### for anonymous read access: "read"
for easier traceability later on.
06 auth-access = write
If you have created a new file, you
must explicitly assign control to SVN. To
As of this writing, Eclipse is unable to tion of available project wizards. SVN | do so, check the box to the left of the file
use the filesystem to access local reposi- Checkout projects from SVN lets you cre- name; this changes the status to added.
tories directly, unless you build Subver- ate a new Eclipse project from the files
Conflict Management
sion (SVN) with the JavaHL bindings in the repository.
and tell Eclipse to use them by selecting If you need to work on existing PHP If multiple developers edit the same file
Preferences | Team | SVN. The SVN files, copy the project root directory into at the same time, SVN automatically
source code is available from the home- your workspace folder and create a new merges the different versions, provided
page [5]. The Subversion server is easy project with the name of the project the changes relate to different lines of
to configure, and it is typically easy to folder in Eclipse. Right-click the project code. However, if the changes overlap,
use for local repositories. root folder and select Team | Share Proj- Subversion will report a conflict, which
To create a repository for the project, ect to hand over control to the version you will need to resolve manually be-
enter svnadmin create cause SVN is obviously unable to under-
--fs-type fsfs path/to/reposi- stand changes to the source code.
tory]; then, edit svnserve. Subclipse transfers the output from
conf (Listing 1) and passwd the command-line tool to the graphical
(Listing 2) in the conf/ sub- Eclipse environment; for files with con-
directory of the repository flicting changes, an exclamation mark
to configure server access. appears to the left of the file name in the
Type svnserve -d -r path/ list. If you open a file, you will see that
to/repository to launch the SVN has inserted conflict markers such
server. To allow Subclipse as <<<<<<< .mine or >>>>>
to access the repository, you >> .r7 (see Listing 3).
first have to register. To do In Listing 3, the lines above the equals
so, select Window | Open signs reflect the local file content, and
Perspective and change to the the lines below the signs show the con-
SVN Repository Exploring tent from the repository. Eclipse offers a
perspective. The left-hand Figure 2: In the Commit dialog, select the files you want view that juxtaposes conflicting lines in
panel has a window titled to update and add an explanatory comment. Team | Edit conflicts (Figure 3). The but-
SVN Repository that displays tons between the two editor windows
all of the integrated repositories. On the management system. with the different code versions let you
far right of the toolbar in this window, By right-clicking on the file browser, keep the changes from either version.
you will find an Add new SVN Repository you can create a new project. Select New After modifying the code, you can select
button. Press the button and type the | PHP Project. If the PHP Project does not Team | Mark as resolved to tell the ver-
URL svn://localhost in the dialog. Sub- appear directly in the New menu, you sion control system that you have re-
clipse will prompt you to enter the pass- might need to take a detour via Other& . solved the conflict.
word. After doing so, the new repository By right-clicking and selecting Team | The Update function saves you the
appears in the list on the left-hand side Share Project, you can assign the new trouble of checking out the whole reposi-
of the window. project for version control. tory whenever you make a change. Files
If the project you are working on in
Eclipse resides in an SVN repository,
Listing 3: Conflict in SVN
Subclipse will help you to check out:
01 $name = $_GET['name']; 08 <<<<<<< .mine
click File | New | Other& to open a selec-
02 <<<<<<< .mine 09 print "

Hallo, $name. You
are $age years old.

";
03 $age = $_GET['age'];
Listing 2: Access Data
10 =======
04 =======
### conf/passwd
11 print "

Hallo, $name. You
05 $country = $_GET['country'];
[users]
come from $country

";
06 >>>>>>> .r7
testuser = secret ### User /
12 >>>>>>> .r7
07
Password
FEBRUARY 2008 ISSUE 87
41
41
COVER STORY PHP with Eclipse
giving the developer an excellent over-
view of the flow of the script. Besides
breakpoints, the debugger also supports
various single-step modes in which the
cursor follows the execution sequence.
Eclipse supports the Dbg PHP debug-
ger, which is available as a binary ar-
chive [6]. Select the dbg_modules that
match your PHP version; modules often
work with newer releases.
To install the dbg.so PHP module,
Figure 3: The Conflicts view helps developers resolve conflicts quickly. Eclipse juxtaposes the modify the PHP configuration file (php.
changes. ini). To find out where the file resides,
create a test.php file with phpinfo() as
its content, and call the file in your web
browser. Search for php.ini in your
browser and then for the extension_dir
variable in the php.ini file. This tells you
the directory with the PHP extension
modules on your system. Now unpack
the archive you downloaded, and move
the dgb.so-x.x.x file that matches your
PHP version to the module folder. Then,
rename the file to dbg.so.
In php.ini, add the following lines
below the position you just found:
Figure 4: What was that command and what parameters does it support? PHPEclipse helps
developers write PHP code by displaying the description of a command. extension=dbg.so
[debugger]
are only downloaded if the version in the ables you have used previously. Brackets debugger.enabled = true
repository is newer than the local work- and exclamation marks are closed auto- debugger.profiler_enabled = true
ing copy. Just as with the Commit func- matically. Eclipse indents code blocks debugger.JIT_host = clienthost
tion, you can trigger an update via the with if instructions or for-next loops. debugger.JIT_port = 7869
dropdown menu for the folder you want Eclipse highlights commands, func-
to update: Team | Update gives you the tions, variables, strings, and some other Now disable the Eaccelerator extension
latest status. The Console window in the code elements with different colors (Fig- (eaccelerator.enable="0") if it exists,
bottom half of the Eclipse window lists ure 5). In Preferences | PHPEclipse Web and comment out the entry for XDebug
the files that have changed. Development, you can customize the (;zend_extension=/usr/lib/
color scheme. This is also where you en- php4/20020429/xdebug.so). Other in-
Go PHP
able line numbering. Eclipse marks lines compatible PHP extensions can be found
The PHPEclipse extension, which you that have been edited since the last save as well [7].
can add via the plugin interface, changes with a blue tag at the start of the line. Change the value of the implicit_flush
Eclipse into a programming environment variable to On. This tells PHP to pass dy-
Error Prone
for PHP. To launch the Update tool, press namically generated content to the web
Help | Software updates | Find and In contrast to syntax errors, which
install, select Search for new features to are indicated by PHP error mes-
install, and then click Next. Then select sages with line numbers to match,
Add remote Site to add a new download errors in the program logic are
URL http://phpeclipse.sourceforge.net/ often difficult to find. The debug-
update/nightly/. ger interrupts the program flow at
The Eclipse/PHPEclipse editor gives predefined points ( breakpoints ).
developers everything they need for The browser shows you the output
rapid programming of PHP code: the script has created up to the cur-
Ctrl+Spacebar completes PHP com- rent point, and the debugger shows
mands (Figure 4). At the same time, you the values of all variables. You
Eclipse displays an overview of parame- can manipulate variables to change
ters and return values, as well as a short the program flow. Figure 5: Syntax highlighting and automatic code
description of the command. Automatic Additionally, the debug cursor formating keep the source code readable. Eclipse
code completion also works with vari- points directly to the line of code, tags syntax errors while you type.
ISSUE 87 FEBRUARY 2008
42
PHP with Eclipse COVER STORY
terpreting process By right-clicking in the debug window,
more closely. Dou- you can stop or restart a session. If you
ble-click the left have used the debugger and no longer
edge of the editor have an active session, the browser will
window to set a display an error page when you call
breakpoint. pages via the local web server. To resolve
Figure 6: The output from the PHP phpinfo() function shows that the Eclipse marks this, delete a cookie called DBGSESSID.
debugger module installation was successful. breakpoints with The debugger needs at least one
blue dots. Break- breakpoint per file, otherwise the debug
server before the script has finished points only work in lines with PHP com- script will just run without stopping. If
running. The advantage of doing this for mands. Now launch the debugger again you want to step through the code, you
debugging is obvious; however, you will (Run | Debug) and start to debug by do not need to set a breakpoint in every
not want to use this setting for a produc- pressing Debug. line: F5 steps through the script line by
tion system because it affects system In the web browser, open the file you line after the first breakpoint. A green
performance. After restarting the web want to debug. To launch the debugger, cursor shows you which command is
server, call test.php again. If the add ?DBGSESSID=1@clienthost:10001 to currently being executed. When stepping
phpinfo() output lists dbg as one of the the address line in the browser. This ac- through the script, the debugger auto-
installed extensions (Figure 6), the tion is only required when you launch matically opens any include files. Press-
debugger installation was successful. the debugger for the first time because ing F6 tells the debugger to skip sub-
You still need to configure the debug- the debugger will set a cookie. The web structures so that you can concentrate
ger tool in Eclipse (Figure 7). Open the browser should now display an incom- on the main loop. At each step, the Vari-
debugger by selecting Run | Debug. In plete page as the breakpoint interrupts ables window gives you read and write
the Configurations panel, select the PHP processing of the file. If your window access to all variable values.
DBG Script entry and click New. A few manager lets it do so, the Eclipse win-
Conclusions
basic settings are needed in the PHP En- dow will move into the foreground. To
vironment tab: In the Remote Debug tab, view the debugger output, open the The ability to see what a program is
check Remote Debug. debug view in Window | Open Perspec- doing step by step helps you find logical
Remote Debug means that the debug- tive, and you will see the active debug- errors. It s fine for exceptional program-
ger will use the web server to access the ging session in the debug subwindow. mers to say they prefer to use their own
pages, and it is the only way to interpret The Variables window in the Debug heads rather than a debugger, but if you
pages that contain PHP code with script- perspective shows all your variables. At know what it is like to spend all night
ing. Remote Debug is the right option the bottom edge of the window, you can looking for a trivial error, you will be
here, even though the server is running see the variable content and manipulate happy for any help you can get.
on the same machine as Eclipse. Remote the content if needed to run the rest of The ability to leverage version control
Sourcepath must point to the project the script with different values. For ar- directly from within the GUI helps to fa-
folder. In the Interpreter tab, enter the rays, you can click on the small array to cilitate ongoing development of existing
path to the PHP binary. You can find the view the elements and their values. The program code. Finally, a feature-rich edi-
path at the command line by typing Breakpoints subwindow lists all the tor accelerates typing, provides a clear
which php. If the interpreter binary does breakpoints and allows you to disable view of the program structure, and helps
not exist, you will need to install the CGI them temporarily. you write readable code.
component for
PHP or build PHP
INFO
with CGI support
[1] Eclipse download:
enabled.
http://www.eclipse.org/downloads/
Now select the
[2] Comprehensive guide to version
project and the
management SVN:
file you want to
http://svnbook.org/
debug in the File
[3] Subclipse: http://subclipse.tigris.org/
tab; click Apply
[4] Eclipse plugins:
and close the
http://www.eclipseplugincentral.com/
debug tool, which
[5] Subversion:
is now fully con-
http://subversion.tigris.org/
figured. Take a
[6] Dbg PHP Debugger Dbg:
look at the source
http://dd.cron.ru/dbg/downloads.php
code of the PHP
[7] Dbg incompatible PHP extensions:
file you want to
http://www.plog4u.org/index.php/
debug and decide Figure 7: The Eclipse debugger accesses pages remotely via the web
Using_PHPEclipse_:_Installation_:
where you need to server that executes the PHP commands. The debugger additionally
_Installing_the_DBG_Debugger
investigate the in- needs the path to the source files.
FEBRUARY 2008 ISSUE 87
43


Wyszukiwarka

Podobne podstrony:
PHP Programowanie Wydanie III phpro3
PHP Programowanie
PHP Programowanie w systemie Windows Vademecum profesjonalisty phppwi
PHP Programowanie obiektowe phppro
PHP Zaawansowane programowanie Vademecum profesjonalisty
PHP profesjonalnie programowanie obiektowe i narzędzia programisty 08 2006
Zaawansowane programowanie w PHP
ASPNET 35 dla programistow PHP
PEAR Programowanie w PHP pearph
2008 11 Opensource owe platformy blogowe [Programowanie PHP]
Linux Apache MySQL i PHP Zaawansowane programowanie lapzap

więcej podobnych podstron