[PHP-NOTES] note 130387 added to class.weakmap
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
WeakMap and SplObjectStorage have different behavior when iterating in a foreach loop. While the key in SplObjectStorage is the numeric index of the inner array, in the WeakMap the key is the object reference:
<?php
class A {}
$a = new A;
$objectStorage = new SplObjectStorage ();
$weakMap = new WeakMap();
$objectStorage [$a] = 1;
$weakMap[$a] = 1;
foreach($objectStorage as $key => $value) {
var_dump($key); // prints: int(0)
}
foreach($weakMap as $key => $value) {
var_dump($key); // prints: object(A)#1 (0) {}
}
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 2001:1308:2067:da00:5147:7d63:6d7b:8699)
----
Manual Page -- https://php.net/manual/en/class.weakmap.php
Edit -- https://main.php.net/note/edit/130387
Del: integrated -- https://main.php.net/note/delete/130387/integrated
Del: useless -- https://main.php.net/note/delete/130387/useless
Del: bad code -- https://main.php.net/note/delete/130387/bad+code
Del: spam -- https://main.php.net/note/delete/130387/spam
Del: non-english -- https://main.php.net/note/delete/130387/non-english
Del: in docs -- https://main.php.net/note/delete/130387/in+docs
Del: other reasons-- https://main.php.net/note/delete/130387
Reject -- https://main.php.net/note/reject/130387
Search -- https://main.php.net/manage/user-notes.php