If you need setters for a class and an object, thatβs fine, we do it all the time, since we need to set the object data. Just don't call it immutable.
Many things in the development world are subjective - our approaches, methodology, etc. - but "immutable" is a pretty solid definition:
"Unchanging":
- Does not change over time or cannot be changed.
If you want an immutable object, this means that it cannot be changed after the instance is created. This is good for things like data from the database, which should remain unchanged throughout the cycle.
If you need to call an object and set or modify data after its creation, this is not an immutable object.
Would you take two wheels off a car and call it a motorcycle?
There is some talk about methods for a "immutable" class that is named without the word "set", but that does not stop their functionality, which is the method that sets the data. You can name it thisDoesNotSetAnything(int $id) and allow the transfer of data in which the object changes. It will be a setter, and therefore the object is mutable.
James
source share