Getting full auto complete with CodeIgniter in Eclipse

CodeIgniter is a brilliant PHP framework and Eclipse is a brilliant IDE, so if I use them both, then it will be super brilliant? Not quite. In CodeIgniter, if you want to use one of the many fabulous libraries, such as the Database Active Record class, then you need to load it (using autoload or manually) but that happens at run time. What this means is Eclipse (and most IDEs for that matter) don’t know what $this->db is which means you don’t get auto complete.

This isn’t really a problem, but it’s a great place to have a couple of key strokes here and there, and we programmers are all about laziness efficiency. In order to enable auto completion, you need to follow these simple steps (I assume you already have Eclipse installed and a CodeIgniter project set up. I will also assume the default directory structure… if you know how to change it around I assume you’ll know where to find the relevant files)

  1. If you’re running CodeIgniter on PHP4 edit system/codeigniter/Base5.php and if you’re running PHP5, edit system/codeigniter/Base4.php
  2. Find the constructor – It’s the function called CI_Base
  3. Paste the following in at the end of it
$agent = new CI_User_agent();
$benchmark = new CI_Benchmark();
$calendar = new CI_Calendar();
$cart = new CI_Cart();
$config = new CI_Config();
$db = new CI_DB_active_record();
$email = new CI_Email();
$encrypt = new CI_Encrypt();
$form_validation = new CI_Form_validation();
$ftp = new CI_FTP();
$image_lib = new CI_Image_lib();
$input = new CI_Input();
$lang = new CI_Language();
$output = new CI_Output();
$pagination = new CI_Pagination();
$parser = new CI_Parser();
$session = new CI_Session();
$table = new CI_Table();
$trackback = new CI_Trackback();
$typography = new CI_Typography();
$unit = new CI_Unit_test();
$upload = new CI_Upload();
$uri = new CI_URI();
$xmlrpc = new CI_Xmlrpc();
$xmlrpcs = new CI_Xmlrpcs();
$zip = new CI_Zip();

You should now have full autocomplete support for all the CodeIgniter libraries. It’s also easy to do the same for your custom libraries and models.

If you have any comments on this, an easier way to get auto complete to work in Eclipse or one where you don’t need to define everything manually then do share it in the comments