I’m working on a medium sized PHP site. I’m writing “provider” classes for my objects. Basically, if I have a “user” object, I have a “user-provider” class that can request / save users on the database, accepting and returning user objects. I just include the providers and the dependent objects in the files I need them.
I’m wondering if this is a clean way to structure a PHP site without a framework, so I’m interested in hearing from others what the best way to structure a PHP site is without using a framework.
First of all, there’s nothing wrong with using a framework, as long as it doesn’t introduce unnecessary complexity and/or performance penalties for small applications. In PHP, the Symfony and Laravel frameworks are quite good in that regard.
Having said that, if you really don’t want a “foreign” framework, you’ll end up writing your own one anyway. So it’s better to plan for that. Just take you favorite framework and structure your project exactly the way you would do when using that framework. Then, write the glue code (i.e. your own mini-framework) yourself. In case you notice that your project grows bigger than expected, you won’t have much trouble switching to a “real” framework.
However, if you really don’t want to use anything that resembles a framework, because your application is really damn small, just put everything into one file. Within that file, you should of course keep a clean separation between presentation, CSS, application logic etc.! But in a minimal application there’s no need to get into the hassle of multiple files.