开发者

Most efficient way to share functions among several php pages

I have about 10 dynamic php pages which use about 30 functions. Each function is needed in more than 1 page, and every page needs a different subset of functions.

I've been pondering these options:

1- all functions in a single include file: 开发者_JS百科every page loads unneeded code

2- each function in its own include file: too many server requests when loading each page

3 - single include file with conditionals only declaring functions needed based on REQUEST_URI: additional processing when loading each page

4 - one include file per php page, with copies of functions needed by that page: difficult to maintain

How does people handle this scenario? Thanks!


Option 1 is the simplest, the easiest to maintain, and probably quicker to run than options 2 and 3. Option 4 would be very very slightly faster to run, at the cost of being a maintenance nightmare.

Stick with option 1.


throw related functions into a library include. include libraries as needed.

Further, if you spend another 5 seconds thinking about this, that will be 5 additional seconds you've wasted

(In case you don't get what I'm saying, worrying about include optimization is about the 5billionth thing on your list of things you should ever worry about, until such time as a reported performance problem from end users and subsequent profiling tells you otherwise.)


You problem shouts OOP.

Organize code by Classes. Load Classes as needed.

More into PHP OOP.

The impact on your server of loading up 30 functions is despicable compared to the impact of yourself having to maintain a map and possible links to all 30 functions.


Start by putting them all in one include_once'ed file.

If performance becomes an issue, start by installing a PHP accelerator (to cache the parsed PHP into opcodes, so constant re-parsing is avoided) on the server.

When maintenance becomes an issue, break them up by function. You'll still end up with a catch-all "util.inc" or "misc.inc" file, though...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜