Several function file includes in PHP
I read through the related questions and didn't find my answer. This isn't about require
/require_once
or the use of the __autoload
function or even the name of the files.
My company builds large sites and as we've grown, the practice we've grown into is splitting up functions by their relation such as:
inc.functions-user.php inc.functions-media.php inc.functions-calendar.php
Each these files tends to be 1000 to 3000 lines of code. Combining would make them a monster to maintain and more difficult for more developers.
However, in some of our larger sites, we end of with somewhere between 8 and 15 of these individual functions files.
Is including the 15 functions files in the header the best way or should we find a way to combine them? Are 12 includes vs. 5 includes significantly detrimental to the running of our site?
If you care about performance install an opcocde cche like APC which will save the compiled form of the script in memory.
If you don't want to install APC the difference is minimal, yes accessing less files takes less time, but that's not where most of the time is spent. (Especially as the filesystem should be able to cache the scripts (uncompiled) in memory) if they are requested often enough.
Calling include/require function 5 times instead of 12 time is not so different, what is important is content of the included file(s).
Also, include cahchers are well suit for your purpose such as APC or xcache.
I would even suggest to split them into much more files. Look at the MVC pattern, or other frameworks, they are extremly splitted, so you easily can maintain "only" parts, without worrying about destroying something, as long as you follow your structure.
Some points to consider that I think about also
- Rasmus Lerdorf has said frequently that "you shouldn't have more than about five includes". I can only assume that he knows what he's talking about, because he made PHP. I am skeptical about the feasibility of this, however. Especially on large projects.
- I've found that it's better for development and milestones to make life easier on your developers. If that means separate files, then that's a good idea.
- If you're worried about CPU usage or bandwidth, there are probably more obvious bottlenecks than liberal use of include. Un-optimized functions are a good way to make the app faster, and paying attention to images and css or js files is a good way to reduce bandwidth.
With vanilla PHP it is generally better to use as few include files as possible, but of course that makes maintenance a pain. Use an opcode cache such as APC and the performance problem will pretty much disappear. Also, 12 files isn't a very large number of includes, compared to the large MVC-frameworks and other libraries. Keeping the functions separated in a logical structure is the best way by far.
精彩评论