Welcome Guest, Not a member yet? Register   Sign In
Cost of CI instance?
#1

[eluser]CodeIgniterNewbie[/eluser]
I'm working on a helper file and realizing that a lot of the functions in there need access to a CI instance for one reason or another (e.g. access to the config, access to the models, etc.). Is there going to be a performance hit due to this?

Am I better off trying to put these helper functions in, say, a "common" controller (it's the controllers that need access to these helper function) since the controller already has access to config, languages, models, etc. (these are usually already autoloaded)?
#2

[eluser]Pascal Kriete[/eluser]
There is no notable performance hit.

Quote:Am I better off trying to put these helper functions in, say, a “common” controller (it’s the controllers that need access to these helper function) since the controller already has access to config, languages, models, etc. (these are usually already autoloaded)?

In principal it's a really simple system. We create an instance of the controller class that the url tells us to use. When you load something, we internally instantiate that library or model and attach a reference to the controller object.

Calling get_instance() simply returns a reference to that object. So after $CI =& get_instance(), $CI and the controller's $this are references to one and the same object.

If this is something you truly need on every request, creating a base controller would work. You could also create a library to hold your methods, and then call get_instance() once in the constructor. But I would only do that with groups of functions that share an overlying concept, not to dump random methods.

In this case organization will give you higher returns than looking for small performance benefits. Helpers exist for a reason, it's perfectly fine to have a handful.
#3

[eluser]CodeIgniterNewbie[/eluser]
Thanks. The helper functions are all related to custom validation. The validation function depend on a number of things, like config, DB, language, etc. I think organizing all these under 1 helper file is good enough organization.

So, doing a get_instance does not add to the memory cost or anything like that, right?




Theme © iAndrew 2016 - Forum software by © MyBB