CodeIgniter Forums

Full Version: More controllers good for modularity, bad for performance?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Of course it's nice to have different classes/controllers performing specific tasks e.g. email, login etc.

But it also means I will have to load these controllers whenever I need to use them. Every time I do that they are being instantiated which is bad for performance.

So I guess my best bet would be to load them in specific methods that require them instead of the constructor?

PS. Im also very reluctant to autoload things, for now I just have session library there and I would rather load them in specific classes/methods.
Optimally breaking requirements down into classes and controllers is part art and part science. Robert Martin (aka "Uncle Bob") is a renowned software developer that writes and talks a lot about objection orientation. You can find some of his videos on YouTube. But he doesn't address CodeIgniter specifically.

Your best bet may be to load them in methods that require them, but will you call those methods more than once? CI won't load them twice, but it will take some number of microseconds to decide not to. Smile

I also don't autoload anything.
Not autoloading is a good thing for performance.

For separating things into more organized classes that each deal with a single topic - yes! do it. While there might be some minimal impact on performance, it won't amount to anything real-world. Your database queries will hurt your performance before your number of classes will 9 times out of 10. And for those other times, there's always caching which is simple to do in CI.

It's better in this case to take the small performance hit and maintain the ease of development, IMHO.
@RobertSF and kilishan: Thank you!
Make your contrlollers to load only when needed. Coz loading all of them at the same time will decrease the performance.