Some of my PHP domain objects have properties which should be set from outside the object, but only by certain client objects. How can I go about enforcing this?
I am using setters within the class, and provided I can find a way to discriminate between different callers, I’m sure I could do some checking in the setter. This seems a bit convoluted though. I get the feeling that I’d be violating an OO design principle (perhaps “tell, don’t ask”?).
I imagine this is a reasonably common scenario. What am I missing?
It strikes me as a bad design in this case. You’d violate the single-responsibility principle of the class. Instead, refactor your code into more than one class, but use composition to attach optional properties.
For example:
Where
IExtendedPropertyis an interface for the custom property data.