---------- Forwarded message ---------- From: Yeti <yeti@xxxxxxxxxx> Date: Fri, Jul 11, 2008 at 2:08 PM Subject: Re: Question regarding OO To: Eric Butera <eric.butera@xxxxxxxxx> I understand my error in thinking now. Apple can't extend Tree. Oak, Evergreen or Willow extend Tree. I thank you all for helping me to understand. On 7/11/08, Eric Butera <eric.butera@xxxxxxxxx> wrote: > > On Fri, Jul 11, 2008 at 12:03 PM, Yeti <yeti@xxxxxxxxxx> wrote: > > <?php > > > > /* > > > > I have a question here regarding object orientation in PHP and any other > > language. > > > > Let's start with some hypothetical code: > > > > */ > > > > // <--- 1st CLASS > > > > class ParkingLot { > > > > var size; // size in sq feet > > > > var max_number_of_cars; // how many cars there is space for > > > > function __construct($size, $max_cars) { > > > > $this->size = $size; $this->max_number_of_cars = $max_cars; > > > > } > > > > }; > > > > cpark = new ParkingLot(50000, 17); > > > > // <--- 2nd CLASS > > > > class ParkingSpace extends ParkingLot { > > > > var ps_ID; // ID of the parking space ( 1 .. max_number_of_cars ) > > > > var occupied_by; // ID of the car on the parking space > > > > var st_time; // Unix time stamp set when the car parks > > > > function __construct($car, $id) { > > > > $this->st_time = time(); > > > > $this->occupied_by = $car; > > > > $this->ps_ID = $id; > > > > } > > > > }; > > > > /* > > > > OK, so i got a parking lot class and its subclass parking space. > > > > Now the question: > > > > I want a list of occupied parking spaces, where do I put that? One might put > > it into the ParkingLot Class, but wouldn't that be a recursion (a child > > instance in the parent class)? > > > > */ > > > > $occ_parking_spaces = array(); > > > > $occ_parking_spaces[] =& new ParkingSpace('Joes Car', 8); > > > > $occ_parking_spaces[] =& new ParkingSpace('My Prshe', 17); > > > > $occ_parking_spaces[] =& new ParkingSpace('Toms Caddy', 4); > > > > /* > > > > After a while a method to print all occupied spaces is necessary. Correct me > > if I'm wrong, but I usually add it to the ParkingLot class. > > > > class ParkingLot { > > > > // previous code ... > > > > var $occ_parking_spaces = array(); > > > > function print_occ_spaces() { > > > > var_dump($this->occ_parking_spaces) > > > > } > > > > }; > > > > What bothers me here is that I'm actually having a list of child classes in > > the parent class, is there a better/correct way to do this? > > > > I hope I made myself clear. > > > > Greetz, > > > > Yeti > > > > */ > > > > ?> > > > > > Listen to what everyone else has said so far. Look up IS-A versus > HAS-A. Favor composition over inheritance. You want your parking lot > to have spaces, but spaces don't have to be in a parking lot. > > http://www.javacamp.org/moreclasses/oop/oop5.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php