I've been developing in PHP for a while now but only recently made the switch over to an OOP approach.
One question that keeps cropping up for me is 'how far' to go with the OOP stuff, specifically in terms of execution speed and memory resources etc.
For example, let's say I have 2 objects, User and Listing
Listings are always linked to a individual user. UserId is a property of Listing so I know which user it relates to. Once in a while, within a Listing method I need to access a single property of the related User.
As far as I can see (please advise if not) I have 3 options for how to accomplish this.
Create a new user object and access the relevent propery via $user -> theProperty
Make the required property a local property of Listing and populate this when Listing is initialised (e.g. via a sql join)
Query the database directly to retrieve the property of User required via the user ID
It seems to me that options 1 & 2 obide by OOP rules more stringently, but have performance hits due to initialising a whole object simply to retrieve 1 property. Option 3 would be the least memory intensive but sidesteps OOP altogether.
In addition, in terms of populating objects at creation, most of my objects get most of their properties populated via one 'fill' method shortly after being initialised (hence requiring only 1 DB query). Would this generally be considered best practise, or would it be more advisable to use individual methods to get these properties, populating as and when they are needed?
I realise there may not be "correct" answers to this, but could anyone give an advice on best ways to approach this situation?
Many thanks Nick