Jump to content

Advice on OOP


Cine

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
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.