GlassCasket Posted August 18, 2007 Share Posted August 18, 2007 So I'm about to build a quoting tool which will use MySQL as a database. The quote builder will fetch and display 250+ items. Also, I will need to access the items image & long description along with a few small tid bits. So I was wondering, should I store each item into an object and do a call like '$item_one->get_desc();"? Is that too maybe objects floating around? I was thinking this would be a good idea so you wouldn't query the database multiple times as all the pertinent information would be stored into the object. Also, would storing each object into an array be advisable? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/ Share on other sites More sharing options...
keeB Posted August 18, 2007 Share Posted August 18, 2007 <?php class Quote { private $db; public function Quote($name) { $this->db = new DatabaseConnection($username, $pass); $this->name = $name; //probably should be some validation here } public function fetch() { return $this->db->query("select quote from quotes where name = .. "); //i would probably build another layer of abstraction here, so I don't have to write queries. } } ?> That's probably how I'd do it. Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-327311 Share on other sites More sharing options...
GlassCasket Posted August 19, 2007 Author Share Posted August 19, 2007 Alright, thanks for the reply. But would you mind explaining a bit what is going on in your class? I don't really know what goes in $name, the item name? And what you're telling me is that I shouldn't store each item into an object, right? Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-328104 Share on other sites More sharing options...
keeB Posted August 19, 2007 Share Posted August 19, 2007 How about I answer your question with an example of how I would use my class... <?php $q = new Quote("GOOG"); // asumes this is a stock quote print_r($q->fetch()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-328178 Share on other sites More sharing options...
corbin Posted August 20, 2007 Share Posted August 20, 2007 Assuming the query method of the DBConn class is an alias to mysql_query, then that would simply say RESOURCE ID #<number>. The only way that would work would be if it was either pushing every thing into an array in $q->fetch, or if it returned a row set, in which case it would only show the first result set. Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-329251 Share on other sites More sharing options...
keeB Posted August 21, 2007 Share Posted August 21, 2007 OK, say your DBConn::Query method looked like this. (Keep in mind, this is a really generic, simple example.) <?php function query($q) { $ret = array(); $res = mysql_query($q); while($data = mysql_fetch_array($res)) { $ret[] = $data; } return $ret; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-329417 Share on other sites More sharing options...
pl_harish Posted August 27, 2007 Share Posted August 27, 2007 Between objects and arrays its a simple equation.. You use arrays as long as the data is manageable with just an array.. manageable in the context of the programmer understanding easily what is where, and be able to quickly draw a flow chart of how data travels within the code if asked. People use objects sometimes even for one field of information.. which could easily be done with arrays.. but they do objects because they expect to have more fields in the near future.. if things are arrays, you will have to spend more time increasing the number of fields handled, because simply arrays are not as organized as objects. $user->isLoggedIn() is more sensible and feels easy to upgrade than isUserLoggedin($username), though both will typically have the same kind of implementation,.. and actually the object way will have more code to type to create the user class..instance it, etc., It entirely depends on what the programmer wants to do.. and how much time he/she has. If professional code is what you are looking for, do object for all data that has child data (or at least will have in future) Theoretically, if things go beyond 2 dimensions in an array, objects are used.. because arrays beyond 2 dimensions are too unfriendly.. considering we want to understand and follow our code after sometime without taking a painful code walk. regards, Harish. www.floresense.com www.harishpalaniappan.com Quote Link to comment https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-335186 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.