class problem

Discussion in 'PHP' started by fluxman, Oct 12, 2007.

  1. #1
    I've simplified my problem:
    I have a simple mysql table set up.

    Bands:
    id, name, description

    Venues:
    id, name, address

    Events:
    id, band_id, venue_id, date



    I'm using php5, and like to stick strictly to OOP...but I've been away from OOP for a while and am having issues about class design.

    The website I'm creating has a page to list ALL bands, and a page to view just one band (so you can see further info).

    At the moment i have 2 classes
    One called Band, which deals with getting the band info from the database, and then a bunch of getter functions.
    And another called Bands, which get's ALL bands from the database and stores them in an array of Band objects.

    I also have a similiar 2 classes for Gigs. (ignore the venues class for now)

    So, this means if I just need to access one band for a page, i can use the Band class....but if i want to list all bands, then I can use the Bands class (which deals with an array of Band objects).

    I think this is an ok way of doing it...until it comes to implementing the Gigs class.

    For example, say if I have one Band object ($myband) and I want to find out $myband's next gig, then i'm thinking i have 2 options
    either create functions:
    a) $nextgig = $myband->get_next_gig() ---- (returns a gig object)
    or
    b) $nextgig = $gigs->get_next_gig($myband_id) ---- (returns a gig object)


    then i'd have something like

    echo $nextgig->get_venue();
    echo $nextgig->get_date();


    which of the best 2 options is easiest / makes more OOP sense? and is there a better way of designing this problem from an oOP point of view?
    the trouble with a) is that $myband would need access to the Gigs Class...yet a) makes more sense "logically" to me ....but then again, b) seems more encapsulated..... It's not that I can't actually do this, I've done it before using various methods...i just want to know the best "OOP" way.

    many Thanks for any insights!
     
    fluxman, Oct 12, 2007 IP
  2. rob_v

    rob_v Peon

    Messages:
    72
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Id do I personally would use b.

    IMO the gigs class should just know about gigs - give it an id - and have it return the object, done.

    If you use a - then the myband object which knows about bands - is then going to have to go out and get gig info as well.

    You can do it either way I suppose.
     
    rob_v, Oct 12, 2007 IP