Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/
Share on other sites


<?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.

Link to comment
https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-327311
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-329251
Share on other sites

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;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-329417
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/65551-to-use-objects-or-not/#findComment-335186
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.