CodeIgniter Forums
Common functions - best practices - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Common functions - best practices (/showthread.php?tid=22207)



Common functions - best practices - El Forum - 09-01-2009

[eluser]m40[/eluser]
I need to have a few functions available to multiple controllers and I was wondering where functions like these should reside.

To be more specific, I have a function called _encrypt_password() and two controllers that make use of it. I have placed the function in MY_Controller, but I was wondering if that would be considered a good practice.


Common functions - best practices - El Forum - 09-01-2009

[eluser]bretticus[/eluser]
See Helpers in manual.


Common functions - best practices - El Forum - 09-01-2009

[eluser]brianw1975[/eluser]
another option would be to put it in a helper then you could load that helper only when it is needed (twice) instead of having that function registered on every page where you use MY_Controller


Common functions - best practices - El Forum - 09-01-2009

[eluser]m40[/eluser]
I thought about this, but my understanding is that helpers should be reusable pieces/modules and the function in question is very application specific and I don't know...


Common functions - best practices - El Forum - 09-01-2009

[eluser]bretticus[/eluser]
[quote author="m40" date="1251837183"]I thought about this, but my understanding is that helpers should be reusable pieces/modules and the function in question is very application specific and I don't know...[/quote]

If your method is likely to be used by every controller, than putting it in MY_controller seems appropriate.


Common functions - best practices - El Forum - 09-01-2009

[eluser]brianw1975[/eluser]
[quote author="bretticus" date="1251838013"][quote author="m40" date="1251837183"]I thought about this, but my understanding is that helpers should be reusable pieces/modules and the function in question is very application specific and I don't know...[/quote]

If your method is likely to be used by every controller, than putting it in MY_controller seems appropriate.[/quote]

I concur, and, just as you (m40) said, the _encrypt_password() is used in only 2 controllers, not site wide. Given that, loading a helper with that function in just those two controllers is more resource frugal than putting it in MY_Controller - which, as I said, will define and allocate memory for that function everytime the MY_Controller is used. Not that big of a deal when you are talking one visitor. but scale that up to 500 or 1000 and it will eat up more memory, and then extend that awareness from just one function to 5, 10, 15 or 20 functions over the entire site... and well, you'll see why other frameworks become very bloated and have overly large foot prints, kinda like this response.


Common functions - best practices - El Forum - 09-01-2009

[eluser]m40[/eluser]
I see your point! Thanks!