I have a basic MVC setup with helper classes containing utility methods, several of these are likely to be used when loading a view, in both the view itself, the controller and sometimes, but not often, the models. I use autoloading to find the helper files I need, so the class is only ever included once, then, in theory the file shouldn’t get searched/included again, that way I don’t have to put include statements in the script and (i believe) the file isn’t loaded unless the class is used.
My question is what’s likely to be the most efficient way to make calls to these helper classes? Should I make all methods in a given helper static, or maybe use a singleton pattern for the helpers or should I just create a new instance wherever it’s used (e.g. an instance in the view an instance in the controller…) ?
thanks for your input
Correct. That’s how autoloading works.
Efficient, well, what’s efficient? Maybe using C/C++ and compiling the stuff. Anyway, let’s try:
No.
No.
Yes. If you only use them from time to time, that’s very efficient because you can make use of anything as you see fit.
In case you use the same object more often, pass it around, inject it into other functions as it’s needed.
This way you will get the most benefit from autoloading.
Helper object that can have multiple helper you can pass around even, will autoload on first access (lazy loading):