Req #65719 [Opn->Wfx]: Register callbacks to an object's lifespan.
| From: | nikic@php.net | Date: | Fri, 20 Sep 2013 10:38:55 +0000 |
| Subject: | Req #65719 [Opn->Wfx]: Register callbacks to an object's lifespan. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-181712@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=65719&edit=1
ID: 65719
Updated by: nikic@php.net
Reported by: metamarkers at gmail dot com
Summary: Register callbacks to an object's lifespan.
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Class/Object related
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
I think you're looking for a WeakMap here: http://de2.php.net/manual/en/class.weakmap.php
Previous Comments:
------------------------------------------------------------------------
[2013-09-20 04:04:51] metamarkers at gmail dot com
Description:
------------
There already exists __destruct(), but if you need to clean up mappings that use
an object's identifier to cache results etc, there isn't a smooth way of doing
this.
It would be nice to tie a callback to an object's lifespan.
The catch would of course be that you can't reference the object in the callback
because of circular references, or maybe you can, I'm not that intimate with
PHP's garbage collection.
Test script:
---------------
$x = (object)[];
register_destructor($x,function($x){
// extra cleanup operations with $x,
// or instead an spl_object_hash() could be precalculated and closed over
// if using $x would cause circular death
});
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=65719&edit=1