Object hierarchy advice

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Object hierarchy advice

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Hey, Jenk, what does NB mean?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
Post Reply