Object hierarchy advice
Posted: Mon Nov 28, 2005 9:24 pm
I'll start off by saying I'm not new to OOP, but I'm the first to admit that my grasp of the entire idea isn't as strong as I'd like to admit. I've written very heavily oriented OOP applications, but I am always "guessing" on how to use my objects. Let's take for instance a shopping cart system. Let's also assume I have a class that handles user's logging in, and the user class is serialized into the session. The user class itself contains all the credentials needed to validated the user.
Now let's also assume I have written a class to handle tracking items (a "cart" class). I would access it's API like $cart -> add(item, quantity) and $cart->remove(item, quantity). Now I could have the user class instantiate the cart as needed, and access the cart through the user object
$user -> cart -> add(item, quantity)
but I'm completely new to the whole hierarchy thing, I've always forced myself to find a way to never have classes instantiating each-other (besides the database abstraction class). The other option I see is using extends. For some reason it seems like the extend syntax is pointless(to use on something like this). So what is the de facto, or I should say "best" way to do this kind of thing? I'm guessing the user object instantiating the cart class.
Now let's also assume I have written a class to handle tracking items (a "cart" class). I would access it's API like $cart -> add(item, quantity) and $cart->remove(item, quantity). Now I could have the user class instantiate the cart as needed, and access the cart through the user object
$user -> cart -> add(item, quantity)
but I'm completely new to the whole hierarchy thing, I've always forced myself to find a way to never have classes instantiating each-other (besides the database abstraction class). The other option I see is using extends. For some reason it seems like the extend syntax is pointless(to use on something like this). So what is the de facto, or I should say "best" way to do this kind of thing? I'm guessing the user object instantiating the cart class.