funeralcrasher: (Default)
Say I have an object called ListStudents which queries a database for all known students. My first (procedural) impulse is to loop through the dataset and store each row in pre-formatted html rows. But something tells me there is a better way of handling it with OOP. Can someone help me?

Date: 2006-07-28 03:02 pm (UTC)From: [identity profile] bedivere.livejournal.com
I dunno what language you're working in; but, the general OOP approach is not do have an object called ListStudents (thats a behavior really), but rather to have a Student class, which has all the fields from the table. Then you have some kind of data access class (say StudentDAO, for example, DAO=data access object), which has a method called getAllStudents (or whatever you wanna call it). That class then retreives the data from the DB and returns a list of Student Objects. Then, from where ever you wanna retreive the students in your app/program, you instantiate an instance of StudentDAO, and call StudentDAO.getAllStudents(). Then you can loop through the list of Student objects, and get out all the properties you need for display in HTML or anything else. All of that probably seems a bit much to do something so simple, but it makes for good OO design. I hope that made some sort of sense, I haven't had my coffee yet :)

Date: 2006-07-28 03:28 pm (UTC)From: [identity profile] bedivere.livejournal.com
Oh... I see you're using PHP. Which I don't use.. but this article seems to get at what I was saying:

http://www.devarticles.com/c/a/PHP/Code-10x-More-Efficiently-Using-Data-Access-Objects-Part-1/

Profile

funeralcrasher: (Default)
funeralcrasher

June 2020

S M T W T F S
 123456
78910111213
141516 1718 1920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 26th, 2025 06:13 am
Powered by Dreamwidth Studios