note 129369 added to class.weakmap
| From: | Anton | Date: | Tue, 02 Apr 2024 20:19:30 +0000 |
| Subject: | note 129369 added to class.weakmap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221268@lists.php.net to get a copy of this message | ||
Keep in mind that if Enum case is put as a key to WeakMap, it will never be removed because it will
always have unless 1 reference to it under the hood (not sure why, probably because enum is
basically a constant).
Therefore, both WeakMaps below will always have key Enum::Case. However, you still are able to unset
it manually:
$weakMap = new WeakMap();
$object = Enum::Case;
$weakMap[$object] = 123;
unset($object);
var_dump($weakMap->count()); // 1
///
$weakMap = new WeakMap();
$weakMap[Enum::Case] = 123;
var_dump($weakMap->count()); // 1
///
$weakMap = new WeakMap();
$weakMap[Enum::Case] = 123;
unset($weakMap[Enum::Case]);
var_dump($weakMap->count()); // 0
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 2001:56a:7a3b:d100:b857:c1b2:f4c:be7c)
----
Manual Page -- https://php.net/manual/en/class.weakmap.php
Edit -- https://main.php.net/note/edit/129369
Del: integrated -- https://main.php.net/note/delete/129369/integrated
Del: useless -- https://main.php.net/note/delete/129369/useless
Del: bad code -- https://main.php.net/note/delete/129369/bad+code
Del: spam -- https://main.php.net/note/delete/129369/spam
Del: non-english -- https://main.php.net/note/delete/129369/non-english
Del: in docs -- https://main.php.net/note/delete/129369/in+docs
Del: other reasons-- https://main.php.net/note/delete/129369
Reject -- https://main.php.net/note/reject/129369
Search -- https://main.php.net/manage/user-notes.php