Should I try to keep all helper functions in the same file (say, all in functions.php) at the cost of reading in unnecessary functions, or store functions in separate files where I'll only include files with functions I need, at the cost of the overhead for including more files? How big of an overhead is there to include separate helper files? I know for images like icons it's faster to join icons together in 1 image, but I'm not sure if the same applies here.
- If you want to store your functions separately from one another, try to group them (by some sort of category). For instance: file-system related helper functions, functions that modify strings, functions that do calculations, etc. The other principle is that when you hit a wall like the one you seem to be hitting, its best to consider a different design pattern. In your case i might say that considering a more objected-oriented approach could serve you well.SQRCAT– SQRCAT2013-12-17 23:00:20 +00:00Commented Dec 17, 2013 at 23:00
1 Answer
This completely depends on the project you're working on - sometimes the one approach may be faster, sometimes the other. But in general, just avoid making lots of global helper functions and put them in appropriate classes instead, as static helpers if need be. Then read up on autoloading and watch as PHP's magic solves the problem all in one go for you - loading and parsing the files automatically as you need them.
If you also use PHP 5.5+ (or an older version with an opcache-like extension) the code will even be precompiled, lowering overhead even further.
Generally speaking some more - once you're starting to worry about the parsing overhead of your code you're usually guilty of premature optimization. In a world where nearly all webservers have quad core hyperthreaded multigigahertz processors and are backed by RAID SATA storage, loading an extra file isn't going to be a realistic problem. Find the real bottlenecks when all the code is done, and spend your optimization time there.