Consider this hypothetical scenario:
I have a Stomach
class, which has a contents
property, to which objects of class Food
should be assigned. One way to do that would be to use a kind of a setter, e.g. setContents($food)
.
But lets suppose that we assign food directly, as in $stomach->contents = $orange
.
Suppose also that whenever a Food
object is assigned to contents
we need to change the object's eaten
property to true
. Is there a way to do that without using a method of an object that it's being assigned to (in this case, the $stomach
's setter)?
Basically, my question is: can we call a method whenever an object is assigned as a property to another object? Also, even if it is possible, is it bad design? Sorry if this is a stupid question, all of this is pretty new to me.