[thelist] PHP object property getters/setters

Chris Johnston fuzzylizard at gmail.com
Wed Nov 30 22:11:24 CST 2005

On 11/30/05, Andrew Kamm <akamm at demicooper.com> wrote:
> Say I have 3 classes - city, state, customer. A customer object would have
> a
> city object as one of it's properties (where the customer lives). The city
> object would have a state as one of it's properties (the state where the
> city is located). So using public properties, I could access info about a
> customer's state by climbing up the chain:
> $customer->city->state->stateName
Dumb question, but why not just have city and state as attributes (i.e.,
strings) of customer? Why make them into separate objects? What are you
gaining by having the city and the state as separate objects in terms of the
design of your application?

In general, from an OO point of view, the above would be bad. Plus, if the
above is all you are doing, then why not add a getter to customer that
retrieves the state name and another getter that will get the city? To me,
this would seem to simplify the public interface to the customer object.
Yes, it means writing a bit more code, but the code makes logical sense when
it comes to a customer.

On the other side, do any of the following make sense:
a customer has a city; a city has a state?
a customer uses a city; a city uses a state?
a customer contains a city; a city contains a state?

Technically, the above are what the associates that you may be making mean,
none of which really make any sense.



More information about the thelist mailing list