RE: [PHP] Question regarding OO
> -Original Message- > From: Jay Blanchard [mailto:[EMAIL PROTECTED] > Sent: Friday, July 11, 2008 1:51 PM > To: Eric Butera; php php > Subject: RE: [PHP] Question regarding OO > > [snip] > 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. > [/snip] > > By jove, I think he's got it! > > Parking space could be a member variable of parking lot, that would > make > sense. One thing at a time, Jay... one thing at a time. ;) Todd Boyd Web Programmer -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Question regarding OO
[snip] 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. [/snip] By jove, I think he's got it! Parking space could be a member variable of parking lot, that would make sense. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Fwd: [PHP] Question regarding OO
-- Forwarded message -- From: Yeti <[EMAIL PROTECTED]> Date: Fri, Jul 11, 2008 at 2:08 PM Subject: Re: [PHP] Question regarding OO To: Eric Butera <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> wrote: > > On Fri, Jul 11, 2008 at 12:03 PM, Yeti <[EMAIL PROTECTED]> wrote: > > > > > /* > > > > 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(5, 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
Re: [PHP] Question regarding OO
On Fri, Jul 11, 2008 at 12:03 PM, Yeti <[EMAIL PROTECTED]> wrote: > > /* > > 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(5, 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
Re: [PHP] Question regarding OO
Corrected code example: (too early in the morning to think) In the above class definition, simply populate the $spaces array with instances of the ParkingSpace class. ~Ted -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Question regarding OO
Object-oriented programming, with it's "class" and "object" approach, is meant to model real life more closely than functional programming. In, a "parking space" is physically inside a "parking lot", but a parking space is not a subclass of a parking lot. It's not a variation or mini parking lot. It's an entirely different entity. A parking lot consists of parking spaces, but a parking space is its own thing. In other words, don't extend ParkingSpace from ParkingLot. They should be separate classes entirely. As for the recursion... no, that won't be a problem. Each ParkingSpace you're instantiating is a separate instance, and won't be related to the parent ParkingLot in anyway way internally. In the above class definition, simply populate the $spaces array with instances of the ParkingSpace class. ~Ted On 11-Jul-08, at 9:03 AM, Yeti wrote: 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(5, 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 */ ?> -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Question regarding OO
On Fri, 2008-07-11 at 18:03 +0200, Yeti wrote: > > /* > > 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(5, 17); > > // <--- 2nd CLASS > > class ParkingSpace extends ParkingLot { Here's the point of your comfusion. ParkingSpace is not really a ParkingLot is it? I mean you could say a parking lot having a single parking space qualifies, but not really. I see you problem being similar to apples versus apple tree. You wouldn't do the following (would you?): Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Question regarding OO
size = $size; $this->max_number_of_cars = $max_cars; } }; cpark = new ParkingLot(5, 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 */ ?>