views:

76

answers:

4

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.

A: 

This is a good starting point: http://www.phpguru.org/static/ApplicationStructure

$_PATHS["base"]      = dirname(dirname(__FILE__)) . "/";
$_PATHS["includes"]  = $_PATHS["base"] . "includes/";
$_PATHS["templates"] = $_PATHS["base"] . "templates/";
$_PATHS["pear"]      = $_PATHS["base"] . "pear/";
$_PATHS["logs"]      = $_PATHS["base"] . "logs/";

But basically what you end up with... is a framework :)

David Titarenco
It is debatable whether this is preferable on big projects, let alone small projects. In recent frameworks the contrary strategy "configuration by convention" has proven to be more successful, rather than defining separate configuration variables for every kind of string concatenation. :-)
vog
Yeah, I definitely agree.. Convention over Configuration (and MVC) are really the "in" thing to do right now. Although even using something like a ridig MVC framework is debatable for online applications, imo. But the OP doesn't want to use a framework, so in that case doing things one way or another is really his prerogative.
David Titarenco
Indeed, but for a simple project CoC is even more advisable, so recommending 5 configuration variables where none is needed seems a bit odd to me. Any simple solution will do as well, as long as you use `dirname(__FILE__)` and don't hard code absolute paths.
vog
Boo for using global variables (if path was super global, fine. But it looks like it is from convention, but is not)... Why not use constants (considering that these paths should change at run-time)?
ircmaxell
+2  A: 

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, I believe the Zend framework is 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.

vog
A: 

Model view controller is also nice to have. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Matt Williamson
A: 
vog