Organizing my functions - Should I create multiple files?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frozax
Forum Newbie
Posts: 4
Joined: Tue Jul 11, 2006 1:40 pm

Organizing my functions - Should I create multiple files?

Post 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!
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Organizing my functions - Should I create multiple files

Post 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.
frozax
Forum Newbie
Posts: 4
Joined: Tue Jul 11, 2006 1:40 pm

Post 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 ?
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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++.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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++.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :(
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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!!!! :evil:
Post Reply