Organizing my functions - Should I create multiple files?
Moderator: General Moderators
Organizing my functions - Should I create multiple files?
I want to organize my php code in functions and I'll be calling different functions depending on the page shown of the web site. Nothing new here.
I wonder which way I should use:
1) One big php file with all my functions and I call the ones I need.
2) Each function (or group of functions) is in its own php file and I "require" the needed files before calling the functions.
I am a bit worried of the server spending time/CPU/resources opening the php file(s) with the 2nd method. However, I'd prefer this solution, because my code will be arranged in a better way, and I'll be more efficient in developing.
Another quite similar problem: the site will contain a list of games, I'll have datas per game (such as name, screenshot link, description...). If I want to separate my informations for each game, should I:
1) put them all in a unique php file (with stuff like $gamename["id"] = "Game Name";)
2) put them each in a different php file (same syntax)
3) put them in .txt/.dat files that I'd open and parse in php (using fopen/fclose).
Will the server have too much work because of the multiple "requires" or "fopens".
I hope I've been understandable! Thanks!
I wonder which way I should use:
1) One big php file with all my functions and I call the ones I need.
2) Each function (or group of functions) is in its own php file and I "require" the needed files before calling the functions.
I am a bit worried of the server spending time/CPU/resources opening the php file(s) with the 2nd method. However, I'd prefer this solution, because my code will be arranged in a better way, and I'll be more efficient in developing.
Another quite similar problem: the site will contain a list of games, I'll have datas per game (such as name, screenshot link, description...). If I want to separate my informations for each game, should I:
1) put them all in a unique php file (with stuff like $gamename["id"] = "Game Name";)
2) put them each in a different php file (same syntax)
3) put them in .txt/.dat files that I'd open and parse in php (using fopen/fclose).
Will the server have too much work because of the multiple "requires" or "fopens".
I hope I've been understandable! Thanks!
Re: Organizing my functions - Should I create multiple files
Three choices:frozax wrote:I am a bit worried of the server spending time/CPU/resources opening the php file(s) with the 2nd method.
1. Run an opcode cache (eAccelerator, Zend Optimizer, etc), and the server impact from that choice will be eliminated thanks to the opcode cache speeding that, and other things up.
2. Throw more hardware at it.
3. Benchmark both, and see what the difference is. Perhaps also find more substantial benchmarks that are more worthwhile to optimize. (This is the one I recommend).
By doing #3, not only do you find the correct solution, you also learn about your app beyond the simple binary question of "Does multiple includes slow the site down?", to "Where can I deeply improve my application response time".
This is the same issue as the first question, so it has the same answer.frozax wrote: Another quite similar problem: the site will contain a list of games, I'll have datas per game (such as name, screenshot link, description...). If I want to separate my informations for each game, should I:
1) put them all in a unique php file (with stuff like $gamename["id"] = "Game Name";)
2) put them each in a different php file (same syntax)
3) put them in .txt/.dat files that I'd open and parse in php (using fopen/fclose).
Will the server have too much work because of the multiple "requires" or "fopens".
I hope I've been understandable! Thanks!
Thank you both for answering.
Just a quick remark about the following:
Just a quick remark about the following:
Isn't PHP an interpreted language? I mean nothing is compiled, but code is read and executed at run time? So if I have a php file with only one function and no "global code", it won't take time to be interpreted if the function is never called... right ?bokehman wrote:they each need to be compliled as called which is quite time consuming.
This is one of those "Yeah, but not exactly" items.frozax wrote:Isn't PHP an interpreted language?
You can call it one, because the user doesn't run the compiler - so it is interpreted by the server.
However..
There *is* a compilation step, and that significant detail means that code is read, then compiled, then run. Since it does so for every script, every time, that adds processing time. (In exchange for an easy to use language that doesn't require a compiler).frozax wrote:I mean nothing is compiled, but code is read and executed at run time?
If you use an opcode cache, you get the best of both worlds. It caches the compiled code, so you get the speed of compiled code, with the ease of use of a (true) interpreted language.
There is some cost. It has to do a file check (if you do an include_once, its actually 5 system calls, I think). Then it has to parse the file (not the same as compiling). Next, it has to add the function to its stack of available functions.frozax wrote:So if I have a php file with only one function and no "global code", it won't take time to be interpreted if the function is never called... right ?
Thats far different from "it won't take time... if the function is never called". Granted, the function not being called *does* reduce the impact, but including it anyways is a non-zero cost.
Well I guess it all depends on the job at hand. Have a look at thisRoja wrote:In my experience, I would have put PHP faster than Java, and slower than c++.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Wow that's really interesting. I like seeing things like this. I wish ASP was on that list to compare with PHP although that's been done over and overbokehman wrote:Well I guess it all depends on the job at hand. Have a look at thisRoja wrote:In my experience, I would have put PHP faster than Java, and slower than c++.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
I wanted to view that link BUT OUR STUPID PROXY SERVER KEEPS BLOCKING OUT USEFUL SITES. CRAP!!!!bokehman wrote:Well I guess it all depends on the job at hand. Have a look at thisRoja wrote:In my experience, I would have put PHP faster than Java, and slower than c++.