I am stuck in a general OOP problem and cannot find the right way to talk about my question.
I want to create a class that gives me an object that I can write once, save it for storage, and then cannot change the properties. (for example: invoice information - after storage, this should be unchanged). Not all information is available immediately; during the life cycle of an object, information is added.
What I would like to avoid is that when you try to write exceptions, they fly out of the setters, because it seems that you are offering a contract that you do not intend to store.
Here are some ideas that I have reviewed so far:
- Pass any information about the entry in the constructor. The constructor throws an exception if data is already present.
- Creation of several classes in the inheritance tree with each class representing the object at some stage of its life cycle, with the necessary setters, where necessary. Add a command interface for all read operations.
- Silently cancel any inappropriate entries.
My thoughts on this: 1. Makes the constructor very unstable, usually a bad idea. 2. An explosion of complexity and does not completely solve the problem (you can call the installer twice in a row, within the same request) 3. Easy, but the same problem as with exceptions; this is all a big cheat for your customers.
(Just FYI: I am working in PHP5 at the moment - although I suspect this is a common problem)
source
share