Object hierarchy advice
Moderator: General Moderators
Object hierarchy advice
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
How about a special case: an Unknown User/Guest?
Code: Select all
Guest extends User {
//overload user-only functions with useless functions
//but keep cart stuff
}- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
You could structure you cart class to extend the user class, then when the user has entered the "shop" you can have something like:
The constructor can then 'steal' the properties from the $user object so you can now use them in the $cart object.
Code: Select all
<?php
//NB: $user is the user object.
$user = new User();
$cart = new Cart($user);
?>- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US