Skip to content

Implement WeakMap#4882

Closed
nikic wants to merge 2 commits into
php:masterfrom
nikic:weakmap
Closed

Implement WeakMap#4882
nikic wants to merge 2 commits into
php:masterfrom
nikic:weakmap

Conversation

@nikic

@nikic nikic commented Nov 1, 2019

Copy link
Copy Markdown
Member

@krakjoe krakjoe added the Feature label Nov 1, 2019
@ntzm

ntzm commented Nov 4, 2019

Copy link
Copy Markdown
Contributor

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

nikic commented Nov 5, 2019

Copy link
Copy Markdown
Member Author

@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

ntzm commented Nov 5, 2019

Copy link
Copy Markdown
Contributor

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

mvorisek commented Nov 11, 2019

Copy link
Copy Markdown
Contributor

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

nikic commented Dec 4, 2019

Copy link
Copy Markdown
Member Author

@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

mvorisek commented Dec 5, 2019

Copy link
Copy Markdown
Contributor

This is of course somewhat roundabout.

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

@nikic

nikic commented Jan 3, 2020

Copy link
Copy Markdown
Member Author

RFC accepted. Merged as d8c9902.

@mvorisek

mvorisek commented Feb 2, 2020

Copy link
Copy Markdown
Contributor

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants