snoopy76
snoopy76

Reputation: 305

Mix of PHP functions and classes in one file?

Are there any strong technical reasons not to combine a list of functions and classes in one PHP file? Or combine several classes in a single file?

I have generally put each class in its own file as a way to keep things "clean." And if I needed to be not-OOP, then maybe I group related functions into a single file. However, I've seen code where everything is just piled into one file. Aside from it not being organized, are there other dangers to this practice?

Upvotes: 1

Views: 1074

Answers (3)

Anther
Anther

Reputation: 1844

The danger is that you're probably making your codebase harder to maintain, organize, and keep version controlled.

The only reason I've seen people try to force every class into one file is that they used something like notepad++ to develop and it made opening files and jumping around classes/functions very slow.

Files in development are a lot like functions/classes and such as that maintenance is easier the smaller and more organized you make them.

Upvotes: 0

jtavares
jtavares

Reputation: 449

Separating classes is considered good practice and is enforced if you intend do use any PSR standard.

There is no technical reason not to do this, but you might find you code easier to maintain if things are modular and organized.

Sometimes I need to place a bunch of classes in the same file, usually when i need to declare interfaces for SOAP services or exeptions, anyway, its still "organized" as these 3-line-of-code classes are better off clustered in one file then scattered all over you codebase.

For functions, you should declare them all in one file for the same reason, but if you have a bunch of them (50+) consider grouping them in any way on different files.

Upvotes: 0

Phil
Phil

Reputation: 164913

The only technical reason not to do so would be to support PSR-0 (autoloading).

This enforces a one class per file standard. This does not apply to global, user-defined functions.

Upvotes: 2

Related Questions