-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Implement WeakMap #4882
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Implement WeakMap #4882
Conversation
I really like this, really like this. However, is there any chance of removing the limit of only allowing objects as keys? I would like to be able to do something like: $map = new WeakMap();
$map[5] = 'foo';
$foo = new stdClass();
$map[$foo] = 'bar'; |
@ntzm What use case do you have in mind for mixing object and non-object keys in the weak map? Supporting this is going to make the implementation a lot more complex (right now objects are stored as integers, so we can't easily support other integer keys), and it seems like a semantically pretty odd thing to do to me. The "weak" part of the map only applies to objects, so also allowing other key types with non-weak semantics seems odd/misleading. |
My case is something like this: I have a decorator class that caches the result of method calls, and the method argument takes any type. At the moment it works by serialising and hashing the argument, and using that as an array key. This is to avoid adding a reference to the object with I can understand your reasoning completely, I think the best solution for my problem is to use Thanks! |
It would be great if the |
@mvorisek It's possible to store an object with a destructor inside the weak map, which will effectively result in such a callback. A dummy example:
This is of course somewhat roundabout. |
You are right and I think this is enought for now. |
RFC accepted. Merged as d8c9902. |
@nikic wdyt about doctrine/orm#7998 (comment) , is there currently any option to register for destruct event and possibility to cancel it? It seems like needed if the target object does not / can not implement a custom __destruct method. |
> Added WeakMap. Refs: * https://wiki.php.net/rfc/weak_maps * php/php-src#4882 * php/php-src@d8c9902
> Added WeakMap. Refs: * https://wiki.php.net/rfc/weak_maps * php/php-src#4882 * php/php-src@d8c9902
Implementation for https://wiki.php.net/rfc/weak_maps.