colder / php-weakref

PECL extension that implements weak references and weak maps in PHP
http://pecl.php.net/weakref
Other
36 stars 13 forks source link

Observe gc events for the weakref? Would it work? #16

Open Wes0617 opened 9 years ago

Wes0617 commented 9 years ago

imagine the following:

$x = new WeakRef($obj);
$x->onLoss(function() use($map){
    echo "the object was garbage collected";
    $map->remove($weakref);
});
// or even...
$x->onBeforeLoss(function($object){
    echo "the object is about to be garbage collected";
    $map->save($object->serialize());
    // $weakref->acquire(); -> triggers "you aren't allowed to acquire a strong reference here"
});
colder commented 9 years ago

I think this would be good to have indeed. I need to reimplement most of weakref for php7 as the internals have changed (I'm not even sure weakref is possible anymore). Therefore it might be available only for php7 at first.

Wes0617 commented 9 years ago

it's ok php7 is the future and it's happening now! :D

shoghicp commented 9 years ago

:+1: for php7 support and this feature, now I can remove all the hacks around this!

pinepain commented 8 years ago

@colder look how I implement notify callbacks functionality in my code in php_weak_referent_object_dtor_obj() at php_weak_reference.c:L126, the only drawback is if exception thrown in original dtor and in any of notifiers, all further notifiers will not be called.

colder commented 8 years ago

That makes sense, but don't you have to handle the case where the callback reads the reference and stores it elsewhere?

pinepain commented 8 years ago

I follow python's weakref.ref's notify appoach where notify callback get called with a weakref.ref object as a first arg after referent object destructed.