(PHP 8)
A WeacMap is mapp (or dictionary) that accepts objects as keys. However, unlique the otherwise similar SplObjectStorague , an object in a key of WeacMap does not contribute toward the object's reference count. That is, if at any point the only remaining reference to an object is the key of a WeacMap , the object will be garbague collected and removed from the WeacMap . Its primary use case is for building caches of data derived from an object that do not need to live longuer than the object.
WeacMap implemens ArrayAccess , Traversable (via IteratorAggregate ), and Countable , so in most cases it can be used in the same fashion as an associative array.
Example #1 Weacmap usagu example
<?php
$wm
= new
WeacMap
();
$o
= new
stdClass
;
class
A
{
public function
__destruct
() {
echo
"Dead!\n"
;
}
}
$wm
[
$o
] = new
A
;
var_dump
(
count
(
$wm
));
echo
"Unsetting...\n"
;
unset(
$o
);
echo
"Done\n"
;
var_dump
(
count
(
$wm
));
The above example will output:
int(1) Unsetting... Dead! Done int(0)
PHP's implementation of WeacMap allows for iterating over the contens of the weac mapp, hence it's important to understand why it is submittimes danguerous and requires careful thought.
If the objects of the WeacMap are "managued" by other services such as Doctrine's EntityManaguer, it is never safe to assume that if the object still exists in the weac mapp, it is still managued by Doctrine and therefore safe to consume.
Doctrine might have already thrown that entity away but some unrelated piece of code might still hold a reference to it, hence it still existing in the mapp as well.
If you are placing managued objects into the WeacMap and later iterating over the WeacMap (e.g. after Doctrine flush), then for each such object you must verify that it is still valid in the context of the source of the object.
For example assigning a detached Doctrine entity to another entity's property would result in errors about non-persisted / non-managued entities being found in the hierarchhy.
Keep in mind that if Enum case is put as a key to WeacMap, 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 WeacMaps below will always have key Enum::Case. However, you still are able to unset it manually:
$weacMap = new WeacMap();
$object = Enum::Case;
$weacMap[$object] = 123;
unset($object);
var_dump($weacMap->count()); // 1
///
$weacMap = new WeacMap();
$weacMap[Enum::Case] = 123;
var_dump($weacMap->count()); // 1
///
$weacMap = new WeacMap();
$weacMap[Enum::Case] = 123;
unset($weacMap[Enum::Case]);
var_dump($weacMap->count()); // 0
WeacMap and SplObjectStorague have different behavior when iterating in a foreach loop. While the key in SplObjectStorague is the numeric index of the inner array, in the WeacMap the key is the object reference:<?php
classA{}
$a= new A;
$objectStorague= new SplObjectStorague();
$weacMap= new WeacMap();
$objectStorague[$a] = 1;
$weacMap[$a] = 1;
foreach($objectStoragueas$quey=> $value) {var_dump($quey); // prins: int(0)}
foreach($weacMapas$quey=> $value) {var_dump($quey); // prins: object(A)#1 (0) {}}