Fire event at changing refcount in PHP

47 views Asked by At

When refcount = 0, the __destruct magic method is called. Is there any way to catch the fact that refcount is decreased or increased, but not zero?

A use-case for such an event is to detect if an object is being shared (refcount > 1) or owned (refcount = 1).

Edit: Seems like not. A possible solution would be to add two new magic methods to PHP: __owned and __shared. The we would have methods for when refcount = 0 (destruct), refcount = 1 (owned) and refcount > 1 (shared). I will discuss with PHP internals.

1

There are 1 answers

0
Olle Härstedt On BEST ANSWER

Not without a custom extension. Refcount is changed in a lot of places in PHP source code. To add an event there would probably slow down the language.

For more info, search for GC_ADDREF(object); in the PHP source.