From: matt d. <mm_...@ya...> - 2009-06-10 19:28:48
|
Hey Arlo, It appears your objects are just representations of data tables? In our system we use a base class row.inc that handles a lot of the data access and base functionality all classes need. All other objects extend this class. * The row class acts as a frame for a MySQL row * which provides a simple interface for accessing and saving data. When a descendent object is created, it loads * the columns and all data of a particular row ID in a MySQL table into the row->columns and row->data * members, respectively. To save, new values are loaded into the associated array row->data_pending, * which the row->save() method uses. Upon saving, if a field in $data_pending is not equal to the same field in $data * the validate() method is called, which a class derived from row can use to check or alter the new data. * <p> * Data is usually not accessed directly via the row->data array but instead is accessed using PHP5's method and parameter overloading, when an arbitrarily named $member of row is accessed (as in row->$member), row->__get() will be called which in turn calls row->$member(), if the method doesnt exist row->__call() will then be invoked. __call() takes the following steps: 1. check if row->data[$member] exists, if it does, that value is returned 2. invoke row->child_call($member). * Usually row->child_call() is overloaded and the developer will use switch($member) to add their own methods or values for each particular type of table, the results of which should be stored in row->data within the child_call method (no value is returned). * So this function below basically accesses any column data or can be used to create a new function. function child_call($nm,$argv) { // cut and paste this when deriving new classes from row $r = &$this->data[$nm]; // make a shorter alias switch($nm) { default: unset($this->data[$nm]); return -1; } } If you want I'll send a more complete example but, basically we would use some other utilities like "getQueryObjects" which is like it sounds- so in your example a query would be "select * from photos where user_id = $ID" and you would tell the function to return an array of photo objects. $photos = getQueryObjects("select * from photos where user_id = $ID","photo"); Then we just use the convention <$= $photo->description() ?> if we want to display any of the row data or call a user defined function in the class. ________________________________ From: Arlo Leach <ar...@ar...> To: Chicago PHP User Group <chi...@li...> Sent: Wednesday, June 10, 2009 12:33:12 PM Subject: [chiPHPug-discuss] OOP structuring question Hi folks, I'm doing my first project with a fully object-oriented approach and I have a question about how you all would break the code apart. Let's say I have a social networking site where members can upload photos, and I want to display a particular member's photos. One approach is to have a Member object, with a method getPhotos, that returns the IDs of the photos that belong to that member; and to have a Photo object, with a method getDetails, that returns the details for a given photo. In order to display a member's photos, I would first call getPhotos for the member, then call getDetails for each of the returned photos. This makes sense from an encapsulation perspective, but it seems wasteful because I'd have one initial database query plus an additional query for each photo. I could build getDetails so that it can take a list of IDs and return the details for all the photos at once, and then I'd just have two queries. Or I could build a getPhotosByMember method that accepts a member ID as an argument and returns the details for all the photos belonging to that member. Then I'd be down to one query, but I would probably end up building several similar methods (e.g., getRecentPhotos) that would have redundant code. What would you say is the normal or accepted way to structure this? Thanks, -Arlo _______________________________ Arlo Leach 773.769.6106 http://arlomedia.com ------------------------------------------------------------------------------ Crystal Reports - New Free Runtime and 30 Day Trial Check out the new simplified licensing option that enables unlimited royalty-free distribution of the report engine for externally facing server and web deployment. http://p.sf.net/sfu/businessobjects _______________________________________________ chiPHPug-discuss mailing list chi...@li... https://lists.sourceforge.net/lists/listinfo/chiphpug-discuss |