One of the functions of an application I manage is to operate a fleet of vehicles. I have an object
vehicle with which I can do tasks that are vehicle-related;
setDriver(), etc. I also have a collection of
vehicles with which I can do tasks that are multiple-vehicle related;
Until now, when I am creating my collection of vehicles, I have been using
PDO::fetchAll with the argument
PDO::FETCH_CLASS to return
vehicle objects into my list of
vehicles. This has advantages, as when traversing the collection, I can use well-known methods to view and edit individual attributes in bulk.
However, I have been noticing that often times we have been creating collections where we compute member attributes that are only of relevance to the current collection. For example
$vehicle->priority helps staff decide which vehicle to use based on the condition of all the vehicles in just this collection. Another is
$vehicle->service_due which when computed makes sure not all vehicles get booked in for a service at the same time. To me, these dynamically computed variable are still object attributes, but I wouldn't expect to view/edit them on a single instance.
In addition, there are attributes which useful in the collection but redundant in an individual instance. The
$vehicle->driver_ID is stored as an integer in the vehicle table in the database. I can either get a seperate collection of
drivers and find the driver by ID, or
JOIN the ID with the
drivers table to get the driver name, and populate the
Finally, there are often many attributes which are just never needed in a collection, which makes me wonder how efficient it is to use the whole object in collections.
Is it good practice to have a base object which is extended just for the purpose of being a member of a collection? If not, how should I be doing it?!
(the development environment is php and mysql, but I guess the question is platform agnostic)