The Well-Tempered PHP Developer
The first plugin you will need for serious PHP work is named, appropriately enough, PHPEclipse. PHPEclipse has been around since 2002, and although the current stable version (1.1.8) is from 2006, there is work currently on version 1.2, and there has been a steady flow of updates, so the project still is quite alive. Getting PHPEclipse is easy; simply use the Eclipse update method, and add a new remote site (see Resources).
PHPEclipse provides not only basic editing facilities, but it also adds syntax coloring and bracket matching for easier reading; code folding, so you can hide a block or function; parameter hints and tooltips—for example, if you don't remember the parameters for the stristr() function, a little pop-up will remind you; and syntax checks (if you make a syntax error, you will get a wavy red underline at the place of the error and pop-up help, Figure 3). PHPEclipse also offers debugging (with either XDebug or DBG) and version control (CSV or SVN)—more on this below.
When you edit a PHP source file, several shortcuts and functions can speed you along. Need to find the declaration for a certain function or variable? Right-click on any reference to it (or press F3), and you will be taken there. If you are unsure about a certain PHP function, pressing Shift-F2 produces a manual—although you usually can get by with hovering the mouse over the function name.
For more prolix coding, there are several formatting functions. You can re-indent any portion of code simply by selecting it, then right-clicking and choosing Format, or by pressing Ctrl-Shift-F. You can turn lines into comments (and vice versa) by right-clicking and choosing Source→Toggle Comment, and all selected lines will get // added in front. (From now on, I skip the shortcuts; unless you are a die-hard Ctrl and Shift fanatic, you probably will use the mouse menus all the time.) Adding or removing larger comments (for example, ones like /* ... */) also is simple with a right-click, then selecting Source→Add Block Comment or Remove Block Comment. A Refactor function can help you change a variable or function name globally; there's no excuse for shoddy names anymore.
PHPEclipse is fully configurable. On the main menu, go to Window→Preferences, and select PHP. You can set your own specific preferences for most of the features I've covered (and even more that I didn't touch on here), so you can set up project standards. If more people are working on the project though, make sure everybody uses the same set of parameters. It's no fun having to reformat other people's code just because of a tabbing configuration difference. Collaboration and version control plugins are discussed below.
When your code is ready, you get Run As..., Debug As... and Profile As... commands. You can create profiles (including runtime parameters, environment variables, directories and more) and use them later with a single click. The results of the run will appear on a console, integrated within Eclipse.
When do you test your code? After everything is done? How quaint and old fashioned! Modern development methodologies suggest an iterative way of working, which combine developing automated test cases even before the actual programming is done. Having the tests available before actual development starts ensures quick feedback after any change, and it also provides design-level documentation, for each test serves as an example of what the code should do. Even more important, putting all the tests together in a test suite provides for regression testing—before any new code is committed, all pre-existing tests should pass. If a programmer makes any mistakes, changing the way a function should have worked, a well-designed test will catch the problem and alert you.
This way of programming has been named test-driven development (TDD) and is a part of many modern agile development techniques. The basic idea is simply preparing an automated test (automated means it can be run on its own, without users having to do anything, which implies that running the same tests several times a day is no chore) that exercises your code and tests the results it produces by checking assertions that are either true or false. If any assertion fails, some piece of code isn't doing its expected thing. Writing (or at least planning) the tests before writing code, makes the developer pay attention to code requirements and modularity—two important quality factors.
There are several tools for testing, generically named xUnit—for example, JUnit is used for Java development, cppunit for C++, PHPUnit for PHP and so on. Although the specific details logically differ between tools, they actually are quite similar. For our purposes, we work with SimpleTest, which is a plugin that provides PHPUnit tests within Eclipse.
SimpleTest is available as open-source code, and its latest version is 1.0.1 (from April 2008). You can download it from SourceForge (see Resources) and install it with Eclipse. After you install and configure it, a new option will be added to the Run As... menu, allowing you to execute PHP unit tests. You can run tests on their own, so you can test only a single routine, or you can build more complex test suites, so you can run lots of tests at the same time. You probably will use individual tests while coding and suite tests before uploading any code.
When testing, a simple console with a colored bar will show up. Red means some test failed (your code doesn't do what was expected), and green means your code passed all tests (Figure 4). If you get a red bar, you can click on the offending test name, and you will be taken directly to the problematic test code (Figure 5).
Due to space constraints, I won't go into how to write tests or use mock objects; check the documentation for more information. In any case, for each project you work on, you should create a second, parallel, test project. (Of course, use version control for it, as well.) Getting used to automated testing takes some time, but the rewards are high, and you may even become, as it has been said, “test-addicted”.
- Geek Guide: The DevOps Toolbox
- Download "The DevOps Toolbox: Tools and Technologies for Scale and Reliability"
- Nmap—Not Just for Evil!
- High-Availability Storage with HA-LVM
- Resurrecting the Armadillo
- DNSMasq, the Pint-Sized Super Dæmon!
- Real-Time Rogue Wireless Access Point Detection with the Raspberry Pi
- Localhost DNS Cache
- March 2015 Issue of Linux Journal: System Administration
- Days Between Dates: the Counting