• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Where to load models, helpers, libraries? Autoload? Constructor? Method?

#1
[eluser]CodeIgniterNewbie[/eluser]
Any guidance on where to load models, helpers, and libraries?

If they are loaded in autoload, they are made available to the entire application. What if the entire application doesn't need it? Loading in constructors offer reduced scope, but still the same problem: what if the entire class doesn't need the resource? Loading in the method offers the least scope, but this means you'll have to load the same resource over and over again (once each time you need it).

My concern is that I load things not needed by the application (perhaps the memory footprint becomes too expensive?). Also, loading in autoload or constructor could lead to them being loaded and NOT being used (e.g. due to code changes and forgetting to double check those places).

Suggestions for best practice?

#2
[eluser]Maglok[/eluser]
The best practice is really to consider it before doing it. Me? I only autoload what I use on EVERY page. Database class for instance, I always query with that all the time. Most of the models I load locally (contstructor), in the constructor, while helpers for forms I tend to load in the functions.

There is no real best practice, just best optimization I guess.

#3
[eluser]mddd[/eluser]
The most efficient would be to only load them just before you need them. But that is not very easy to work with.
I think you should autoload stuff when it is used in the majority of your controllers. Load things in the constructor if they are only used in a few controllers. Load very specific code (special libraries etc) in the method where you use them.

Yes, there is a little overhead for loading libraries you don't use. But if you think about it, using a framework like CI also takes overhead. It would be more efficient if you wrote only the php-code that did exactly what you needed! But even so, most people use a framework because it makes life easier.

Same here. Just make a sensible estimate of how often things are used and load them accordingly.

If it makes you feel any better, other things than php code have far more influence on the total efficiency of your application. If you write good Mysql queries, and don't load results that you don't need, that will save a lot more!

#4
[eluser]Jelmer[/eluser]
Personally I try to do as much on demand and as little globally as possible. I do almost always load the form and URL helpers through autoload.

I used to place all libraries/helpers/models that a controller needed for most of its function in the constructor (directly after parent::Controller()). And when they were specific to the method I'd place them at the very top of the method before any other code (to make any "dependencies" clear and easy to locate).

But I've switched to using my implementation of another syntax for CI a while ago and now I don't need to load anything before I use it except for Helpers (which I still place in the aforementioned locations).
For example: I'd use Ci::lib('email')->from() instead of first loading the email class. It will try first to give me $this->email. When that fails it loads the email library (using $this->load->library('email')) and then tries again to return $this->email.
(you can find that "syntax" implementation as a helper here)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.