PHP5: SplObjectStorage garbage collection
I'm using a SplObjectStorage to keep information about managed objects. When my objects get destructed, I would like the SplObjectStorage
to automatically cleanup the objects which have no external references anymore.
I can see only two option开发者_如何学运维s for this right now:
- having the managed object's destructor inform the storage to remove references to it; this is not acceptable as these objects should not be aware of the manager;
- parsing debug_zval_dump() to get the reference count; not acceptable as well, IMHO too "hacky" for a serious application.
Any other ideas?
You can try to implement desired functionality with events. Each managed objects generates an event on destroy and the object manager is subscribed on this event so it will be able to remove a reference to object from the SplObjectStorage.
精彩评论