PHP5: SplObjectStorage garbage collector

I use SplObjectStorage to store information about managed objects. When my objects are destroyed, I would like SplObjectStorage automatically SplObjectStorage objects that no longer have external references.

Now I see only two options:

  • having a managed object destructor, informs the repository about the removal of links to it; this is unacceptable because these objects do not need to know about the manager;
  • parsing debug_zval_dump () to get a reference count; unacceptable, IMHO is too "hacked" for serious use.

Any other ideas?

+4
source share
1 answer

You can try to implement the desired functions using events. Each managed object generates an event for destruction, and the object manager subscribes to this event so that it can remove the reference to the object from the SplObjectStorage object.

0
source

All Articles