You could create a parent class called, say Model like so: abstract class Model { protected static $_featureTable; static public function getAllById($id) { global $db; $items = array(); $db->where(array("userId"=>$userId)); $rows = $db->get(self::$_featureTable)->fetch(0); foreach($rows as $row) { $item = self::getInstance(); $item->setValues($row); $items = $item; } return $items; } abstract static protected function getInstance(); abstract protected function setValues($row); } class Blog extends Model { protected static $_featureTable = 'blogs'; protected static function getInstance() { $self = __CLASS__; return new $self(); } protected function setValues($row) { $this->content = $row'content'; // etc. } } Then, to get a list of blogs: $blogs = Blog::getAllById($id).
You could create a parent class called, say, Model, like so: abstract class Model { protected static $_featureTable; static public function getAllById($id) { global $db; $items = array(); $db->where(array("userId"=>$userId)); $rows = $db->get(self::$_featureTable)->fetch(0); foreach($rows as $row) { $item = self::getInstance(); $item->setValues($row); $items = $item; } return $items; } abstract static protected function getInstance(); abstract protected function setValues($row); } class Blog extends Model { protected static $_featureTable = 'blogs'; protected static function getInstance() { $self = __CLASS__; return new $self(); } protected function setValues($row) { $this->content = $row'content'; // etc. } } Then, to get a list of blogs: $blogs = Blog::getAllById($id).
The only issue I have here is that for the $item->content = $row'content' stuff, these will be different for all features, for example the image class will have a filename where as the blog will have content. Any suggestions here? Other than that, its all good thanks!
– phpNutt Dec 16 '09 at 17:33 @Matt: updated code in response to your comment. Note the addition of the setValues() method. – Lucas Oman Dec 16 '09 at 17:39 Thanks Lucas!
:-) – phpNutt Dec 16 '09 at 21:03 I have emailed you about this code you posted here, could you respond when you have a min please – phpNutt Dec 16 '09 at 23:26.
Maybe you should have a look at some ORM systems like Doctrine or Propel. This will help you a lot with your database object mapping. And I know that at least Doctrine supports inheritances of tables and of course maps this structure also to a class hierarchy (which enables you to implement common methods in the parent class).
You could create a parameterizeable Factory object/function. It would aggregate a 'row_to_object' function and a 'query' object: function row_to_blog( $row ) { return new Blog( $row"id", $row"title" ); } function create_from_query( $query, $row_to_object ) { $objects=array(); foreach( $row as $db->fetch( $query ) ) { $objects=$row_to_object( $row ); } return $objects; } $query=new Query( "blogs", new Where("userid",$id) ); $blogs=create_from_query( $query, row_to_blog ).
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 . I have a class for each but in each class I basically define a similar method that gets all blogs | Friends | images.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.