PHP code completion for anything in Eclipse + PDT

While I prefer to make quick changes to my code in a lightweight text editor, any lengthy coding session usually involves loading up an IDE in order to have access to all of the extra time-saving features. Code completion is one of those hard-to-live-without features that IDEs provide.. here’s a quick rundown showing how to get it working with any library or class (visible to your buildpath) to work under Eclipse + PDT.

The idea is very simple, although it is a bit of a trick as you have to apply it to any/every project you work on. Using PHPDoc’s @property you can reference any variable in a class (even if it hasn’t been declared) and associate it with a PHP Type. PDT does the rest!

For example, this is how you can add code completion to a CodeIgniter project’s controller classes:

Create a custom base controller (ie. MY_Controller — see the CI guide for more info) and at the top, in the class’ PHPDoc, add @property tags for any variable you want to use. Now any controllers you create, that extend MY_Controller, will automatically have code completion enabled for the variables you tag. In the following example, $load, $session and $output will be available.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
/**
 * Base Controller Class
 * @property CI_Loader $load
 * @property CI_Session $session
 * @property CI_Output $output
 */
 
abstract class MY_Controller extends CI_Controller
{
// ...
}

2 thoughts on “PHP code completion for anything in Eclipse + PDT

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Proudly powered by WordPress
Theme: Esquire by Matthew Buchanan.