20 Jan 2010

Getting full auto complete with CodeIgniter in Eclipse

CodeIgniter 31 Comments

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

31 Responses to “Getting full auto complete with CodeIgniter in Eclipse”

  1. Phil Sturgeon says:

    “If you’re running CodeIgniter on PHP4 edit system/codeigniter/Base5.php and if you’re running PHP5, edit system/codeigniter/Base4.php”

    Did you mean to write that? ;-)

    I could not get this working myself. Surely for those classes to be defined ready for instantiation they need to be loaded, included or autoloaded?

    Even if they were all loaded, it might be worth mentioning that it should not be used on live sites, as loading every class on every page load is not going to do anything good for the performance of your application.

  2. Damian says:

    Yes I did mean to write that! And it’s linked to your last concern. As I run all my applications on PHP5 and therefore have all this stuff put in base4.php, the file never actually gets loaded when you run the application, but Eclipse seems to pick it up when you’re coding as everything extends CI_Base.

    And when you say you can’t get this to work, you mean you can’t get the auto complete to work, or you get an error when you run the application (which might make sense if you thought this actually loads the library for you, which it doesn’t, you still need to do $this->load->library yourself)

    Hope that’s cleared things up a bit

  3. Phil Sturgeon says:

    That makes a lot more sense. After putting this into the bottom of Base4.php’s CI_Base constructor, saving and restarting Eclipse I still get auto-complete when writing $this->input.

    What should I be expecting?

  4. Damian says:

    “I still get auto-complete when writing $this->input. What should I be expecting?”

    You *still* get full autocomplete, you mean you had it anyway?

    You should be expecting all the variables and method to appear within the input class in this case once you’ve types “$this->input->”. Unfortunately, due to CI being compatible with PHP4 you also see all the “private” variables and method.

    Also, as you scroll through the list of available methods, or start typing the one you want, you also get the PHPdoc and parameter list for that function, useful if you forget the optional 8th parameter for a method ;-)

    That cleared things up a bit? I might reword a couple of parts of the post to make things clearer

  5. Phil Sturgeon says:

    Sorry I meant “I still get no auto-complete”. Nothing happens, at all, anywhere. It might be that I am being thick (it happens!), or perhaps you missed something from your post. Not sure, but doing what you have said does nothing for me. >.<

  6. Damian says:

    Ok I think I know what the problem is, we’ll call it an oversight on my part. At some point when I’ve been using eclipse, I seem to have changed a setting somewhere which results in autocomplete being automatically triggered, so when I type “$this->input->” autocomplete kicks in.

    I just created a new workspace with all the default settings, put in a fresh copy of CodeIgniter, modified base4.php and set about trying to use autocomplete in the default controller… (after having refreshed the workspace by hitting F5 on the project so it knows about the autocomplete)

    I typed “$this->input->”… waited a few seconds… still nothing. Then I tried to manually invoke autocomplete using Ctrl + space and there it was, the full list of all the methods and variables in the Input class.

    Fingers crossed it will work for you too now :)

  7. Jay says:

    Hi Damian!

    I couldn’t get this working either. Tried mixed with my autocomplete (suggest) settings in Eclipse around, but with no success at all. I have put the code in the Base4.php before the ending bracket inside the CI_BASE function.

    I use Eclipse on XP with v1.21 / 20090920-1017. Do you have any idea on how to solve it? It would be awesome to me!

    Thanks a lot and keep up the good work with the blog!

  8. Damian says:

    Hi Jay!
    Have you tried rebuilding, refreshing or best of all, restarting Eclipse? You need to force it to upate itself, so it knows these classes exist when you hit Ctrl + space to trigger auto complete part way through typing something like “$this->”

    Also, when you say version 1.21 is that the version of Eclipse (which doesn’t seem right based on the build number 20090920-1017)

  9. gg says:

    Hi!

    This auto complete solution works…but i don’t no how to auto-complete my custom model.

    Thanks

  10. Damian says:

    Hi gg, glad it works for you.
    You could be rfering to one of 2 problems when you say “don’t no how to auto-complete my custom model”… Both of which have a solution, but neither of which are what I would say are overly elegant.

    Getting autocomplete of model controllers and variables within your controllers
    To do this, we need to do the same sort of thing as we done for the CI libraries, so if you have a model called “user_model” then add the following to the end of the above code
    $user_model = new User_model();
    If you always alias your model name to be user instead of user_model, then change the above line to be
    $user = new Uer_model();

    Getting autocomplete of CI libraries (and models) within your model
    The problem with getting autocomplete to work within models is that they don’t extend the CI_Base object, so for development, I make all my models extend a MY_Model which extends CI_Base, then when it goes to production, I just remove the extends statement from my model and all is well again (it doesn’t even seem to affect aything during dev)

    I hope one of, or both of, the above points solve your problem =)

  11. Jay says:

    Hi! Reinstalled Eclipse and everythging worked great. Thanks for this and your support This will save me loads of time. Awesome!”

  12. Damian says:

    Hi Jay,
    Glad you finally got it working, think reinstalling Eclipse is a tad extreme, but if it works, it works =)

  13. Nitin Reddy Katkam says:

    Hi!

    I’ve added that code into my Base file but have also added an “if (false)” around it so if my code is run on PHP4 or PHP5, the added code wouldn’t have any side-effects.

    The Ctrl+Space autocomplete works just fine for me, without requiring a refresh or a restart.

    -Nitin

  14. Damian says:

    Hi Nitin
    That’s a great idea about wrapping it in if(false)

    And the reason you didn’t need to refresh or restart is cause of the timings of the workspace aurto refreshing (normally every 5 minutes by default)

  15. buddika@gmail.com says:

    help me with the aptana

  16. Damian says:

    Hi buddika,
    What aspect of this is failing to work with Aptana? Although I can’t guarantee it should work, it should, as Aptana is efffectively Eclipse with some bits thrown in on top

    If you can be more specific then I can help some :)

  17. ismail says:

    i got the following error when i paste in Base5.php above line
    Fatal error: Class ‘CI_Calendar’ not found in /home/rubyonrails/public_html/search/system/codeigniter/Base5.php on line 42

  18. Damian says:

    Hi ismail,
    Are you running PHP 4 or 5? I have a feeling you’ve got PHP 5 which means you need to paste the code into the base4.php file, we don’t actually want this code to get executed (as you can see, it doesn’t work) we just want to trick the IDE into knowing what all these objects (accessed via $this->something) are and what methods they have

  19. asifsomy says:

    “I will also assume the default directory structure… ”

    do u mean that application folder should be in system folder??

    unfortunately this method didn’t work for me. Don’t know the reason. :(

  20. buddika@gmail.com says:

    this works very fine …i already check with net beans IDE .working very well…..even aptana as well

  21. Eclipse Autocomplete für CodeIgniter | Benjamin Mock says:

    [...] viel Zeit. Ein Autocomplete ist für die CodeIgniter Methoden ist daher eine echte Erleichterung. Damian Gostomski erklärt in diesem Post wie es geht. Auch wenn man das Projekt cleanen muss und Eclipse neustarten [...]

  22. ALX says:

    It works for me.
    Thanks for the tips !

  23. buddika@gmail.com says:

    there was a youtube video how to use the net beans IDE for autocomplete

  24. onur says:

    this didn’t work for me either. i tried every single bit of things but no way to make autocomplete for CI work. just as damian mentioned above i created a fresh new workspace with a fresh copy of codeigniter and just changed the Base4.php file then tried $this->input-> and ctrl+space and got the red message saying that “No completions available”. no refresh or restart made any good.

    can it be the reason that i’m using “Eclipse for PHP Developers” without any plugins etc.?

  25. Guil says:

    Great Job explaining !!

    I got it to work right away but the comments helped me solve minor issues without stressing.

    Thank you Damian for putting good content on the web!

  26. Damian says:

    @Guil
    What where the minor issues you encountered? I’m thinking of expending on this a bit in the near future as it’s obvious some parts aren’t clear enough

  27. Guil says:

    Hi Damian!

    Sadly I am still not getting it to work properly

    What I did was add the system folder to my “PHP include path” and that gave me access to using Auto-complete.

    But as soon as I remove it (while leaving your code in Base4.php) the auto-complete drops off.

    I’m not quite sure why this is happening.

    I still have your code placed in Base4.php in both the system foider (next to my application folder) and the referenced library (which is another folder named CI_CORE see example: http://codeigniter.com/forums/viewthread/69098/)

    I’m really interested in seeing your solution – Thank you for doing this!

  28. Georgi Budinov says:

    Hi Damian,

    I found your your post about enabling IntelliSense but I was also unable to do it . So I thought of doing something else – not editing the core class, but extending some of them in the application/libraries folder e.g. the controller – http://georgi.budinov.com/2010/08/codeigniter-and-intellisense-in-eclipse-pdt/

  29. Damian says:

    @Georgi
    One limitation of going it in MY_Controller is that it only works in Controllers. To get it to work in Models, you would need to do thesame for MY_Model, and same for any custom libraries.

  30. CodeIgniter and IntelliSense in Eclipse PDT | Georgi.Budinov.com says:

    [...] For those that are not aware, IntelliSense is a feature of the Eclipse IDE, which allows us to quickly inspect the properties of an instantiated object of some type, function parameters, auto-completion, short description, if available and so on. This is incredibly handy when developing applications, no matter the programming language. When developing with CodeIgniter, due to the style the framework is written, the IntelliSense is not working. I found a post that describes how you could enable it but I couldn’t do it – perhaps it is just me personally that is unable to achieve the goal – see here. [...]

  31. CodeIgniter, Eclipse PDT и използването на IntelliSense | Georgi.Budinov.com says:

    [...] За тези, които не са наясно, IntelliSense е удобство предлагано от Eclipse PDT, което ни позволява бързо да инспектираме атрибутите на инстанцирани обекти от какъвто ида е тип, параметрите на функциите/методите, допълване на кода, кратко описание, ако е налично и други. Това е невероятно удобно, при разработката на приложения, независимо от програмния език. Когато разработваме с CodeIgniter, поради начина, по който е написан framework-a, IntelliSense-а не работи. Намерих един пост, който описва как може да си го пусне човек – може би просто аз съм неспособен да го направя – вижте тук. [...]

Leave a Reply