1--TEST--
2The weak part of the WeakMap
3--FILE--
4<?php
5
6$map = new WeakMap;
7
8// This is doing to be inserted into the map and immediately removed again
9$map[new stdClass] = 1;
10var_dump($map);
11
12$obj = new stdClass;
13$map[$obj] = 2;
14var_dump($map);
15
16unset($obj);
17var_dump($map);
18
19echo "\nDestructor in WeakMap value:\n";
20$obj = new stdClass;
21$map[$obj] = new class {
22    public function __destruct() {
23        echo "Dtor!\n";
24    }
25};
26
27echo "Before unset:\n";
28unset($obj);
29echo "After unset:\n";
30var_dump($map);
31
32echo "\nDestroying map with live object:\n";
33$obj = new stdClass;
34$map[$obj] = 3;
35unset($map);
36var_dump($obj);
37
38echo "\nObject freed by GC:\n";
39$map = new WeakMap;
40$obj = new stdClass;
41$obj->obj = $obj;
42$map[$obj] = 4;
43unset($obj);
44var_dump($map);
45gc_collect_cycles();
46var_dump($map);
47
48echo "\nStoring object as own value:\n";
49$map = new WeakMap;
50$obj = new stdClass;
51$map[$obj] = $obj;
52unset($obj);
53var_dump($map);
54unset($map);
55
56echo "\nStoring map in itself:\n";
57$map = new WeakMap;
58$map[$map] = $map;
59var_dump($map);
60unset($map);
61
62?>
63--EXPECT--
64object(WeakMap)#1 (0) {
65}
66object(WeakMap)#1 (1) {
67  [0]=>
68  array(2) {
69    ["key"]=>
70    object(stdClass)#2 (0) {
71    }
72    ["value"]=>
73    int(2)
74  }
75}
76object(WeakMap)#1 (0) {
77}
78
79Destructor in WeakMap value:
80Before unset:
81Dtor!
82After unset:
83object(WeakMap)#1 (0) {
84}
85
86Destroying map with live object:
87object(stdClass)#2 (0) {
88}
89
90Object freed by GC:
91object(WeakMap)#1 (1) {
92  [0]=>
93  array(2) {
94    ["key"]=>
95    object(stdClass)#3 (1) {
96      ["obj"]=>
97      *RECURSION*
98    }
99    ["value"]=>
100    int(4)
101  }
102}
103object(WeakMap)#1 (0) {
104}
105
106Storing object as own value:
107object(WeakMap)#3 (1) {
108  [0]=>
109  array(2) {
110    ["key"]=>
111    object(stdClass)#1 (0) {
112    }
113    ["value"]=>
114    object(stdClass)#1 (0) {
115    }
116  }
117}
118
119Storing map in itself:
120object(WeakMap)#3 (1) {
121  [0]=>
122  array(2) {
123    ["key"]=>
124    *RECURSION*
125    ["value"]=>
126    *RECURSION*
127  }
128}
129