php-weakref icon indicating copy to clipboard operation
php-weakref copied to clipboard

Observe gc events for the weakref? Would it work?

Open Wes1262 opened this issue 10 years ago • 6 comments

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"
});

Wes1262 avatar Jul 18 '15 07:07 Wes1262

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.

colder avatar Aug 08 '15 10:08 colder

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

Wes1262 avatar Aug 08 '15 16:08 Wes1262

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

shoghicp avatar Aug 10 '15 08:08 shoghicp

@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.

pinepain avatar Jan 11 '16 18:01 pinepain

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

colder avatar Jan 11 '16 22:01 colder

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.

pinepain avatar Jan 11 '16 22:01 pinepain