Object composition and reference
| From: | Paul Smith | Date: | Fri, 06 Sep 2002 21:58:06 +0000 |
| Subject: | Object composition and reference | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-115575@lists.php.net to get a copy of this message | ||
I'm compositing a class based on other classes. The component classes are (this is for a transportation application) fuel and vehicle. The container class is mode (as in mode of transit: rail, bus, etc.). There can be several vehicles and fuels for each mode (many to one). I want to be able to accomplish something like this:
$bus = new mode();
$diesel = new fuel($bus);
$ethanol = new fuel($bus);
$class_a_bus = new vehicle($bus);
$articulated_bus = new vehicle($bus);
In other words, register the fuels and vehicles with the mode object in such a way that I don't have to make them members of said object; in further other words, instead of doing it something like:
$bus = new mode();
$diesel = new fuel();
$bus->addFuel($diesel);
Is there a way I can then reference the components of the mode object without having to know the names I picked for the variables; without having to do something like:
$diesel = new fuel();
$bus->addFuel($diesel, 'diesel'); <-- a handle to reference the globally scoped variable
One kludgy way would be to foreach() the $GLOBALS in a method of the mode class, checking for is_a('fuel'). One of my rules of thumb is to reduce use of the $GLOBALS. Would a more elegant solution be related to PHP's reference counting? I apologize if this is a confusing-as-hell post. I would be more than happy to clarify a point...
Paul
--
Paul Smith <paul@cnt.org>
Center for Neighborhood Technology
Chicago, IL USA