Speed up PHP development in Eclipse PDT with Templates and Snippets

Filed under

Busy schedules, long to do lists, and deadlines make it tough for developers to get familiar with some of the advanced features their tools provide. This is a shame, because some features, like Eclipse Templates, can really reduce coding time and errors.

You already know that Eclipse PDT provides autocompletion and hints for PHP's built in functions. In addition, commenting your code with PHPDoc adds autocompletion and hinting for your application's classes, methods, and variables. If you haven't already discovered, Eclipse PDT also provides autocompletion of PHP's control structures, class definitions, and methods through code templates.

Eclipse PDT Templates

The PHP Developer Tools plug in provides several templates for all of PHP's control structures (if/else, for, foreach, while, switch, etc.) and more. Fire up Eclipse and take a look at Preferences > PHP > Templates. Here are all of the predefined templates available.

The name column indicates what you need to type to insert a template. Context, description, and the template code are all visible here. Some of the names are straightforward but others may take time to memorize. Printing the template list and keeping it nearby might be helpful.

To insert a template, type the name followed by Control + Space bar, select the template, and hit enter/return. Below, I typed 'swi' and then Control + Space bar to add a switch statement.

You must be working in a file that's part of a PHP Project to be able to use these PDT templates!

Once inserted, templates may contain variable fields. The switch template contains two variables (shown with box outlines below), one for the switch statement's variable/expression and the second for the case comparison. Just tab between the fields.

Think of all of the time you'll save and the typos you'll avoid with these templates. If the value or format of any of the templates don't fit with you or your team's coding standards, add new or edit existing templates, and share them with the team by exporting and importing them. Just to be safe, be sure to keep a copy of your exported template XML file as future PDT upgrades may overwrite customizations.

Eclipse Snippets

Most IDE's include a feature to save blocks of code for re-use and Eclipse is no exception. Snippets are suited for code that is not part of an existing template.

Add the snippet to your workspace by selecting Window > Show View > Other... and then select General > Snippets. You can add categories to group snippets however you'd like (ex. PHP, CakePHP, Zend Framework, CSS, Bash, Smarty, etc.). Once you have your categories, add snippets to them.

  1. Open a file and enter a new block or select an existing block of code.
  2. Copy the code block.
  3. Click in the Snippets view, under a specific target category, if you've created categories.
  4. Select "Paste as Snippet..." in the Snippets view menu.
  5. In the "Customize Palette" dialog, enter a name and description.
  6. Use the "Move Up" and "Move Down" buttons to order the snippet in lists as desired.
  7. Click OK

You can also right-click anywhere in the Snippets view and select "Customize" to add a snippet. I haven't worked with variables yet, I assume they work like Template variables do. You can set when snippet categories are displayed so that, for example, PHP-related snippets are displayed only when you're editing PHP files.

Snippets can be exported and imported just like templates. Once I've created and used them for a while, I may share a few snippet categories. Let me know if you've seen any good collections.

Happy coding!