Page 1 of 1
Organizing my functions - Should I create multiple files?
Posted: Wed Jul 12, 2006 9:55 am
by frozax
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!
Posted: Wed Jul 12, 2006 10:01 am
by bokehman
Option 2 should be fine unless you are talking a few hundred functions. The trade off of individual files is they each need to be compliled as called which is quite time consuming.
Re: Organizing my functions - Should I create multiple files
Posted: Wed Jul 12, 2006 10:14 am
by Roja
frozax wrote:I am a bit worried of the server spending time/CPU/resources opening the php file(s) with the 2nd method.
Three choices:
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".
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!
This is the same issue as the first question, so it has the same answer.
Posted: Wed Jul 12, 2006 3:05 pm
by frozax
Thank you both for answering.
Just a quick remark about the following:
bokehman wrote:they each need to be compliled as called which is quite time consuming.
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 ?
Posted: Wed Jul 12, 2006 3:15 pm
by bokehman
frozax wrote:nothing is compiled
Every file is compiled before it is run. This happens every time a PHP file is called and is why PHP is slow compared to java and double slow compared to c++.
Posted: Wed Jul 12, 2006 3:32 pm
by Roja
frozax wrote:Isn't PHP an interpreted language?
This is one of those "Yeah, but not exactly" items.
You can call it one, because the user doesn't run the compiler - so it is interpreted by the server.
However..
frozax wrote:I mean nothing is compiled, but code is read and executed at run time?
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).
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.
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 ?
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.
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.
Posted: Wed Jul 12, 2006 3:33 pm
by Roja
bokehman wrote:frozax wrote:nothing is compiled
Every file is compiled before it is run. This happens every time a PHP file is called and is why PHP is slow compared to java and double slow compared to c++.
Interesting. In my experience, I would have put PHP faster than Java, and slower than c++.
Posted: Wed Jul 12, 2006 4:14 pm
by bokehman
Roja wrote:In my experience, I would have put PHP faster than Java, and slower than c++.
Well I guess it all depends on the job at hand.
Have a look at this
Posted: Wed Jul 12, 2006 5:15 pm
by Chris Corbyn
bokehman wrote:Roja wrote:In my experience, I would have put PHP faster than Java, and slower than c++.
Well I guess it all depends on the job at hand.
Have a look at this
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 over

Posted: Wed Jul 12, 2006 5:25 pm
by RobertGonzalez
bokehman wrote:Roja wrote:In my experience, I would have put PHP faster than Java, and slower than c++.
Well I guess it all depends on the job at hand.
Have a look at this
I wanted to view that link BUT OUR STUPID PROXY SERVER KEEPS BLOCKING OUT USEFUL SITES. CRAP!!!!
