In PHP, what's the best way to track an objects state, to tell whether or not it has been modified? I have a repository object that creates new entities, they are then read/modified by other components and eventually given back to the repository. If the object has changed I want the repository to save the data back to the persistent storage (DB).
I can think of four options:
- Use an internal boolean property called $_modified, which every setter updates when modifications are made (tedious if there are a lot of setters).
- Some horrible ugly hack using serialize() and comparing the strings (I'm sure this is a very bad idea, but I thought I'd add it for completeness)
- Something like the above but with a hash of the objects properties (not sure if it would work with objects that contain other objects)
-
Taking clones of the objects as they come out of the repo and comparing what comes in (more complicated than it sounds, because how do you know which clone to compare the object to?)
or...
Some other clever trick I'm not aware of?
Thanks,
Jack