Confused About Objects...
View Single Post
01-22-2010, 10:03 AM
Join Date: Dec 2009
Confused About Objects...
So, I program in a procedural way for the most part, but have some experience with objects in PHP, Java, and a little C++. For my current project, Im considering making a switch to objects but I'm confused about whether or not this will be beneficial from a performance point of view.
My project deals with online flash cards, and its built in code igniter. The way its set up right now is that I use a function for each thing i need to access, and they are pretty unrelated. So, if I just need to display the title and description of a deck, I might call a function like:
Then, if I'm displaying the deck info along with that deck's tags, votes, comments, individual cards, and some other things, I'll simply call a function for each thing, set the results to an array, and then loop through the array and display those things.
I'm thinking my code would be nicer if I just made a deck object to handle all this, but I'm worried that an Object Oriented approach will use more resources than necessary.
For instance, If I had my deck object, it would take a deckId as a parameter and in the constructor it would get the basic deck info (title, description, dateEdited, etc). Then if I wanted the comments I would call $deckInstance->getComments();
What I'm worried about is that in many cases, like when I update the comments with ajax, I will only need the comments and not the basic deck info. It seems like I'm completely wasting a query in the constructor to get the basic info if i'm only going to use the comments. Is this worth worrying about? Should I not have the constructor get any data, and reserve the basic info for its own method? This seems strange though, for a deck object to exist, and not even store its title or the id of the user who created it.
Also, I have considered getting all the data in the constructor, so that I could access it all easily after making an instance of the object, but I feel like this would be a major waste of resources.
If I go the route where the constructor does not go to the database at all, then I feel like I'm not benefiting much from the Object Approach, I'm just grouping together a series of procedural functions.
Sorry this is so long, but I never know when or how to best use objects. Any thoughts?
Something interesting... If you visit one of my sites today, you'll probably double my unique visitors for the day! Shameless Plugs:
My Octopus Game
My Flash Card Website
View Public Profile
Send a private message to andformore
Find More Posts by andformore