views:

152

answers:

4

I had an idea today (that millions of others have probably already had) of putting all the sites script into a single file, instead of having multiple, seperate ones. When submitting a form, there would also be a hidden field called something like 'action' which would represent which function in the file would handle it.

I know that things like Code Igniter and CakePHP exist which help seperate/organise the code.

Is this a good or bad idea in terms of security, speed and maintenance?

Do things like this already exist that i am not aware of?

+1  A: 

There are many forums that do this. Personally, I don't like it, mainly because if you make an error in the file, the entire site is broken until you fix it.

I like separation of each part, but I guess it has its plusses.

It's likely bad for maintenance, as you can't easily disable a section of your site for an update.

Speed: I'm not sure to be honest.

Security: You could accomplish the exact same security settings but just adding a security check to a file and then including that file in all your pages.

webdestroya
+5  A: 

What's the point? It's just going to make maintenance more difficult. If you're having a hard time managing multiple files, you should invest the time into finding a better text editor / IDE and stop using Notepad or whatever is making it so difficult in the first place!

dkamins
Tom Schlick
+4  A: 

Many PHP frameworks rely on the Front Controller design: a single small PHP script serves as the landing point for all requests. Based on request arguments, the front controller invokes code in other PHP scripts.

But storing all code for your site in a single file is not practical, as other people have commented.

Bill Karwin
This concept seems very similar to what i mentioned (so it has been done before then ;)), aside from the seperating into seperate files. It does seem that the disadvantages of this method outweigh the advantages though.
A single script would get way too large and take too long to laod. The Front Controller is designed to have a single point of entry, which is really what you want to do. Then you delegate control to other files, which allows things to scale.
Brent Baisley
+1  A: 

If you're not caching your scripts, everything in a single file means less disk I/O, and since generally, disk I/O is an expensive operation, this probably can be a significant benefit.

The thing is, by the time you're getting enough traffic for this to matter, you're probably better off going with caching anyway. I suppose it might make some limited sense, though, in special cases where you're stuck on a shared hosting environment where bandwidth isn't an issue.

Maintenance and security: composing software out of small integral pieces of code a programmer can fit inside their head (and a computer can manage neatly in memory) is almost always a better idea than a huge ol' file. Though if you wanted to make it hell for other devs to tinker with your code, the huge ol' file might serve well enough as part of an obfuscation scheme. ;)

If for some reason you were using the single-file approach to try and squeeze out extra disk I/O, then what you'd want to do is create a build process, where you did your actual development work in a series of broken-out discrete files, and issued make or ant like command to generate your single file.

Weston C