Best practices in number of files in app

What is everyone’s thoughts on the number of php files in your web application? Do you keep many files with few operations located in them, or few files with many operations in them? Is there an advantage to one over the other in terms of performance, upgrade ability, etc?

Generally, the more files you load the longer the script runs so having as few files as possible is best from performance point of view.

However, not splitting parts of your code into many files in a large application would be very messy and hard to maintain so it doesn’t make sense to do that. File access is not a huge performance hit so in most cases there’s no need to worry and until it becomes a problem I wouldn’t try to pack everything into one file. Moreover, opcode caches like APC, PHP Accellerator, etc. can speed up includes a lot by caching them in memory.

It’s good to keep each class in a separate file. It makes navigation around your code convenient and you can easily set up class autoloading to avoid unnecessary includes every time you use a class.


I keep all my classes in separate files, and the same goes for interfaces too. My classes are also generally quite small and focused on just doing one thing. I rarely have files with more than a few hundred lines in them. It works well and keeps the code organised.

*Edit: I also do MVC for practically everything if I can help it, and tend to keep my controllers slim, my models simple and my views dumb (very little logic in my views). If I need to do a complex process involving a bunch of models, I tend to create a class to encapsulate the process itself, so I don’t have any complicated logic in my models. My models tend to just have access and setter methods in them, and little logic other than checking for dependencies and things like that.

I also almost never echo html from my classes or from php in general. One of my biggest pet hates is to see people echoing html directly from php, especially within a class or function context.