Frozen models idea/implementation #53875
Replies: 5 comments 2 replies
-
We updated again the proposed solution to Make the stdClass read only also for its content. |
Beta Was this translation helpful? Give feedback.
-
The idea of immutable objects has a very sound basis in Computer Science for improving reliability, and indeed Data Transfer Objects are generally immutable and often the more generalised Data Objects are immutable also. The reasons for this are AFAICS twofold:
In PHP I would guess that an immutable object is essentially a read-only class where all attributes are read-only and set only when the object is constructed. Creating a new database record will mean creating a new read-only Model passing all attributes when it is constructed. When it is saved and you know the uniqueid, you will then need to create a new immutable object containing all the same attributes plus the id. I would imagine that sometimes (perhaps even most times) you might want some fields in a model to be "frozen" and others to be updateable. When you want to update a record, you will need to either create a new immutable version with the changed data, or to create a non-read-only model. And I have a problem with the idea of having to have separate readonly and non-readonly versions of what is essentially the same object for all sorts of reasons. I think I would prefer to be able to choose whether an instance of a specific object class is:
PHP's readonly class or attribute designator is all or nothing. I think that to achieve the above you actually need object methods to validate attribute setting against the class or attribute frozen status (with readonly attributes obviously being permanently frozen). |
Beta Was this translation helpful? Give feedback.
-
@Sophist-UK Thanks for the insights. The idea started from @cosmastech 's need of sending objects that must be readonly. |
Beta Was this translation helpful? Give feedback.
-
@macropay-solutions The solution does not prevent the user from making another trip to db and fetch the model. |
Beta Was this translation helpful? Give feedback.
-
Merged the feature into the free lib v3.4.9 and also in both demos. Who will need it will be able to use it. |
Beta Was this translation helpful? Give feedback.
-
Thanks to @cosmastech for the idea
and to @bulletproof-coding for this example.
We would like to receive some feedback on this draft + demo.
Beta Was this translation helpful? Give feedback.
All reactions