Cine Posted August 9, 2011 Share Posted August 9, 2011 Hi, I need some advice on how I should go about fetching data from a database, taking advantage of OOP principles. I have a table named 'items' in a Database. I've created a an 'Item' class, that holds all my instance variables for a specific item. These instance variables, can be directly associated with the fields I have in the table. I also have an 'ItemAbstraction' class, which is responsible for 'getting' items given an Item ID or name for example. My question is, which of the two methods below would work best? 1) Have a method in the 'ItemAbstraction' class that calls a 'getItems' method in the 'Item' class which queries the database and returns an associative array of items. Then, as I'm iterating through this array... create a 'Item' object for each returned item row, and set the instance variables in that object accordingly. I'll then store this object in my own subject array, which can be returned. 2) To simply have a method that returns the associative array of items returned by a successful database query? 3) Any other ways? Many thanks for the replies in advance. Quote Link to comment Share on other sites More sharing options...
AyKay47 Posted August 9, 2011 Share Posted August 9, 2011 this really boils down to personal preference, both methods wil work, for its simplicity I myself would go with number 2 simply because the task that you want to execute is not dificult and there is no need to over work things..a single method with passable arguments should be all that you need to query a database and ouput results. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 9, 2011 Share Posted August 9, 2011 In my opinion I would determine the solution based upon the following question: Are there additional methods you want to execute against the results? If yes, then creating new objects from the results would make sense. If no, then I see no reason to create new objects out of the results. Quote Link to comment Share on other sites More sharing options...
Cine Posted August 9, 2011 Author Share Posted August 9, 2011 this really boils down to personal preference, both methods wil work, for its simplicity I myself would go with number 2 simply because the task that you want to execute is not dificult and there is no need to over work things..a single method with passable arguments should be all that you need to query a database and ouput results. Hi, appreciate your reply. The reason why I suggested using #1, was that there'll be quite a few 'get...' methods and other database interoperability methods in the 'ItemAbstraction' class and the advantage of them all calling one method in the 'Item' class, is that they can share a single persistent database connection, instead of establishing a new one for each query; which is what #2 would require. Thanks. Quote Link to comment Share on other sites More sharing options...
the182guy Posted August 9, 2011 Share Posted August 9, 2011 The getItems() method would be more appropriate in the ItemsAbstraction class. You could have it return an array of objects using mysql_fetch_object() for example. Unless you need methods from the Item class there would be no need to create an Item object for every record in a list, especially if the stdClass returned by fetch_object() has the same properties as your Item class. When working with a single item in the application, it may be more suitable to instantiate your Item class for it. Quote Link to comment Share on other sites More sharing options...
Cine Posted August 9, 2011 Author Share Posted August 9, 2011 In my opinion I would determine the solution based upon the following question: Are there additional methods you want to execute against the results? If yes, then creating new objects from the results would make sense. If no, then I see no reason to create new objects out of the results. My thinking entirely. Thanks. Quote Link to comment Share on other sites More sharing options...
Cine Posted August 9, 2011 Author Share Posted August 9, 2011 The getItems() method would be more appropriate in the ItemsAbstraction class. You could have it return an array of objects using mysql_fetch_object() for example. Unless you need methods from the Item class there would be no need to create an Item object for every record in a list, especially if the stdClass returned by fetch_object() has the same properties as your Item class. When working with a single item in the application, it may be more suitable to instantiate your Item class for it. Ah I didn't think about the mysql_fetch_object() method. It would suit what I'm trying to achieve perfectly. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.