Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > Question regarding OO
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 20th November 04:17
yeti
External User
 
Posts: 1
Default Question regarding OO



<?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

*/

?>
  Reply With Quote


 


2 20th November 04:17
robert
External User
 
Posts: 1
Default Question regarding OO



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?):

<?php

class AppleTree
{
}

class Apple extends AppleTree { }
?>

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
3 20th November 04:17
ted-talks
External User
 
Posts: 1
Default 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.

<?php

class ParkingSpace {
var $size;
var $num_spaces;
var $spaces = array();
}

class ParkingSpace {
var $ps_ID;
var $occupied_by;
var $st_time;
}

?>

In the above class definition, simply populate the $spaces array with
instances of the ParkingSpace class.

~Ted
  Reply With Quote
4 20th November 04:17
ted-talks
External User
 
Posts: 1
Default Question regarding OO


Corrected code example:

(too early in the morning to think)

<?php

class ParkingLot {
var $size;
var $num_spaces;
var $spaces = array();
}

class ParkingSpace {
var $ps_ID;
var $occupied_by;
var $st_time;
}

?>

In the above class definition, simply populate the $spaces array with
instances of the ParkingSpace class.

~Ted
  Reply With Quote
5 20th November 04:18
eric.butera
External User
 
Posts: 1
Default Question regarding OO


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
  Reply With Quote
6 20th November 04:18
eric.butera
External User
 
Posts: 1
Default Question regarding OO


---------- Forwarded message ----------
From: Yeti <yeti@myhich.com>
Date: Fri, Jul 11, 2008 at 2:08 PM
Subject: Re: [php] Question regarding OO
To: Eric Butera <eric.butera@gmail.com>


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.
  Reply With Quote
7 20th November 04:18
jblanchard
External User
 
Posts: 1
Default 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.
  Reply With Quote
8 20th November 04:18
tmboyd1
External User
 
Posts: 1
Default Question regarding OO


One thing at a time, Jay... one thing at a time.


Todd Boyd
Web Programmer
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Php 5 forum, Apache forum, Iis forum, Functions forum, Classes forum, Librarys forum, Bugs forum, Postgres forum, Mysql forum, Paradox forum, Ms sql forum, Configurations forum, Php.ini forum, Problems forum, Scripting forum, Css forum, General forums, Off-topic talk, Links, Extra forums, Php


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666