Page 1 of 1

Object hierarchy advice

Posted: Mon Nov 28, 2005 9:24 pm
by josh
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.

Posted: Mon Nov 28, 2005 9:52 pm
by Ambush Commander
Well think about it this way.

You have a user.

You have a cart.

The user comes into being when he logs in.

How does the cart come into being?

OOP is all about having an intuitive design that relates to the real world. So think concrete.

Posted: Mon Nov 28, 2005 10:09 pm
by josh
Well I would completely agree that the cart should be instantiated from the user class, but I want users to still be able to add items to their cart without registering for the site. That would mean re-writing the entire user class to allow for guests. Guess I better get to work then.

Posted: Mon Nov 28, 2005 10:10 pm
by Ambush Commander
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
}

Posted: Mon Nov 28, 2005 10:46 pm
by josh
I'll modify the user class, so I don't have to go change the API that uses it, I'll let the userclass function with a user of NULL but it just won't do anything except add stuff to it's cart. Thanks for the advice

Posted: Mon Nov 28, 2005 10:50 pm
by Ambush Commander
Exactly. No problem. (I have to give this one to PoEAA, which gave me the solution to a problem that you had)

Posted: Tue Nov 29, 2005 4:17 am
by Jenk
You could structure you cart class to extend the user class, then when the user has entered the "shop" you can have something like:

Code: Select all

<?php

//NB: $user is the user object.

$user = new User();

$cart = new Cart($user);

?>
The constructor can then 'steal' the properties from the $user object so you can now use them in the $cart object.

Posted: Tue Nov 29, 2005 2:04 pm
by Ambush Commander
Hey, Jenk, what does NB mean?

Posted: Tue Nov 29, 2005 2:31 pm
by josh
NB -
abbreviation for nota bene: used to mark something as particularly important.


???

Anyways I decided to have $user -> cart, easier to deal with the objects that way