Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Split code into multiple files or keep it in as few files as possible? [closed]

Right now I have a script that has reached 400 KB in 4 files (about 3500 lines per file).

Should I split it into more files?

Does having more files have a impact on performance because of multiple require_once calls?

like image 972
Alex Avatar asked Jan 27 '11 16:01

Alex


2 Answers

When using require or include, PHP processes all the files as if it was one big file (imagine copy and pasting the code from the included files into your master file).

The only thing you'd really gain from splitting into separate files is organization. Organization can be huge when working with that much code, so it really depends on what works the best for you.

like image 100
Michael Irigoyen Avatar answered Oct 14 '22 05:10

Michael Irigoyen


I have a large, 13000 line script that I combined from three others, but it's entirely view and user-management related, dealing with output and receiving input. The functions are in another, 8000 line file, and I make sure there's zero bleedover between the two. No HTML in the functions file, no SQL in the main file. Would this be different if I could use objects? Of course. Can't.

So the moral of the story is: It all depends on what makes you comfortable for the script in question. I have one other that is 5 small files because it makes more sense that way; this one makes sense as two large ones.

like image 44
Andrew Avatar answered Oct 14 '22 07:10

Andrew