KevySaysBeNice
[H]ard|Gawd
- Joined
- Dec 7, 2001
- Messages
- 1,452
Hi all!
I'll (try to) keep this short:
I'm working on a project and I'm finding myself wondering how I should be doing stuff in PHP, and if I'm doing things "MVC" or if I'm doing things "OO", or both, or what. Basically, here is an example to illustrate my point:
I am working on a for-fun "craft" website, that lists craft projects I've worked on. In my views, I am doing things like
The problem is, I'm wondering where I should be loading up a "craft" object.
For instance, should I make a Craft class, that contains all of the attributes, getters/setters, etc, for a Craft, and then create a model that basically loads all of the data into a Craft class?
OR, should my Craft class automatically load all of the data itself, so it's 100% self contained. So I do $craft = new Craft(); and a new Craft object is created which automatically (in it's __Construct for instance) does all of the DB stuff to pull in all of the data about the Craft?
Or should I do a combination of the two, so that I can (for instance) do $craft = new Craft(0); - passing in "TRUE" populates the most recent Craft, passing in another ID will populate that crafts ID. So that way, I could do something like (in my model)
Is this how to do it?
Or should I leave ALL DB stuff in the model, and just create a Craft class for storing the data that I pull out of the model?
<3
I'll (try to) keep this short:
I'm working on a project and I'm finding myself wondering how I should be doing stuff in PHP, and if I'm doing things "MVC" or if I'm doing things "OO", or both, or what. Basically, here is an example to illustrate my point:
I am working on a for-fun "craft" website, that lists craft projects I've worked on. In my views, I am doing things like
Code:
foreach($crafts as $craft)
{
<img src="<?php echo craft->getMainImage(); ?>" />
}
The problem is, I'm wondering where I should be loading up a "craft" object.
For instance, should I make a Craft class, that contains all of the attributes, getters/setters, etc, for a Craft, and then create a model that basically loads all of the data into a Craft class?
OR, should my Craft class automatically load all of the data itself, so it's 100% self contained. So I do $craft = new Craft(); and a new Craft object is created which automatically (in it's __Construct for instance) does all of the DB stuff to pull in all of the data about the Craft?
Or should I do a combination of the two, so that I can (for instance) do $craft = new Craft(0); - passing in "TRUE" populates the most recent Craft, passing in another ID will populate that crafts ID. So that way, I could do something like (in my model)
Code:
function getCrafts($start_date, $end_date)
{
//query database to get a list of the crafts that were done within the given date range
// store in $craft_id_list
$craft_list = array();
foreach($craft_id_list as $craft_id)
{
$craft_list[] = new Craft($craft_id);
}
return $craft_list;
}
Is this how to do it?
Or should I leave ALL DB stuff in the model, and just create a Craft class for storing the data that I pull out of the model?
<3