Hey,
I have been designing a site locally in PHP 5, but have come across a few design issues I'd like advice now.
Currently there are three features of the site and each feature has a class . These features are as follows :
- a blog
- a friends list
- a set of images
I have a class for each but in each class I basically define a similar method that gets all [blogs | Friends | images]. I was wondering if any of you know how I could reduce these classes to be much thinner and probably have one class that is generic between all three features for all methods that are the same for each feature. (i.e getAllById($feature, $id)).
An example function for my existing blog class is as follows:
function getBlogsByUserId($userId) {
global $db;
$blogs = array();
$db->where(array("userId"=>$userId));
$rows = $db->get("blog")->fetch(0);
foreach($rows as $row) {
$blog = new Blog();
$blog->id = $row['id'];
$blog->userId = $row['userId'];
$blog->content = $row['content'];
$blogs[] = $blog;
}
return $blogs;
}
Note: I have defined my own class for the DB stuff so don't worry about that.
I've looked at the gateway design pattern but haven't yet found a solution. I also want this to be reusable so if I increase the features to seven or more then I won't have to change much of the class.
Thanks, Matt