Skip to content

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

Closed
wants to merge 2 commits into from
Closed

Implement WeakMap #4882

wants to merge 2 commits into from

Conversation

nikic
Copy link
Member

@nikic nikic commented Nov 1, 2019

@krakjoe krakjoe added the Feature label Nov 1, 2019
@ntzm
Copy link
Contributor

ntzm commented Nov 4, 2019

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';

@nikic
Copy link
Member Author

nikic commented Nov 5, 2019

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

@ntzm
Copy link
Contributor

ntzm commented Nov 5, 2019

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

I can understand your reasoning completely, I think the best solution for my problem is to use WeakMap for objects, and a standard array map for scalar types.

Thanks!

@mvorisek
Copy link
Contributor

mvorisek commented Nov 11, 2019

It would be great if the WeakMap would allow to define a callback which will be invoked before the key/value is garbage collected. The same for WeakMap. It would then allow "a lots of magic" for caches etc.

@nikic
Copy link
Member Author

nikic commented Dec 4, 2019

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

$object = new stdClass;
$map = new WeakMap;
$map[$object] = new class {
    public function __destruct() {
        echo "Object destroyed!\n";
    }
};
unset($object);

This is of course somewhat roundabout.

@mvorisek
Copy link
Contributor

mvorisek commented Dec 5, 2019

This is of course somewhat roundabout.

You are right and I think this is enought for now.

@nikic
Copy link
Member Author

nikic commented Jan 3, 2020

RFC accepted. Merged as d8c9902.

@mvorisek
Copy link
Contributor

mvorisek commented Feb 2, 2020

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

@carusogabriel carusogabriel added this to the PHP 8.0 milestone May 29, 2020
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Aug 16, 2020
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Aug 17, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants