Where would a list fit into oop?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
weismana81
Forum Newbie
Posts: 20
Joined: Mon Feb 07, 2011 3:36 am

Where would a list fit into oop?

Post by weismana81 »

I'm finally making some progress in object oriented programming and I'm really starting to enjoy the theory now that my brain isn't fighting it so much :)

One issue I can't seem to get past is where does the list fit in? I'm sure I'm missing something obvious, but I've been stuck on this for a while. Let's say I have a User class and a Car class. Where would I get a list of cars? Of course I could get the list from either class, but neither "feels" right. Creating a CarList class also feels a little weird. I've also considered just running a query to get the list, iterating over the result, and building a Car object for each record. But the idea of running a query so directly while I'm working so hard to abstract everything seems really backwards.

I know this is a random question, and thanks in advance for taking the time to consider it. I feel like all of the sudden I'm righting really handsome code and I don't want to head down the wrong path with such a fundamental issue.

Any advice or guidance is greatly appreciated! Thanks!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Where would a list fit into oop?

Post by requinix »

Two possibilities as I see it: either you want a list of cars or you want a collection of cars.

The former is best just as an array. There's nothing special about the list, it just has a few cars in it and you're happy. Including a car in the list doesn't really mean anything, it's just that you've now included it. Similarly removing a car doesn't mean anything, you're just taking it out. It's probably what you're dealing with when you do a straight "query" on the Car class, such as "get a list of cars that match this make, model, and year".
A collection is more complicated - it represents some sort of real collection of cars. For example it could be a collection of cars available by a dealership. There, adding cars means that the dealer has something new and probably has to update their ledgers or whatever to reflect it. Removing a car from the collection means the dealer sold it so (a) their inventory has changed and (b) it's now owned by somebody else (and is in their collection now).

For code, ask yourself a fairly simple question: if you made a CarCollection class, what would you want it to do beyond the normal array-like operations (add, find, remove, etc.) Most of the time you'll either answer "nothing important, and the methods could go in the Car class just fine" and should use a list or "well I need this, this, this, and that, and might want to extend it later with a subclass or three" and should use a collection. Personally, if I'm not sure then I'll start with an array and work from there: it's generally easy to go from an array to a class but less so the other way around, and if you don't find yourself needing "collection"-type behavior then you've saved your scripts a fair chunk of time and memory.
weismana81
Forum Newbie
Posts: 20
Joined: Mon Feb 07, 2011 3:36 am

Re: Where would a list fit into oop?

Post by weismana81 »

Super helpful! Thanks so much for the input!
Post Reply