1--TEST-- 2Basic WeakMap behavior (as a map) 3--FILE-- 4<?php 5 6$map = new WeakMap; 7var_dump(count($map)); 8 9$obj = new stdClass; 10$obj->value = 1; 11$obj2 = new stdClass; 12$obj2->value = 2; 13 14$map[$obj] = $obj2; 15var_dump(count($map)); 16var_dump($map); 17var_dump(isset($map[$obj])); 18var_dump(!empty($map[$obj])); 19var_dump($map[$obj]); 20 21$map[$obj] = 42; 22var_dump($map); 23var_dump(isset($map[$obj])); 24var_dump(!empty($map[$obj])); 25var_dump($map[$obj]); 26 27$map[$obj] = false; 28var_dump($map); 29var_dump(isset($map[$obj])); 30var_dump(!empty($map[$obj])); 31var_dump($map[$obj]); 32 33$map[$obj] = null; 34var_dump($map); 35var_dump(isset($map[$obj])); 36var_dump(!empty($map[$obj])); 37var_dump($map[$obj]); 38 39unset($map[$obj]); 40var_dump($map); 41var_dump(isset($map[$obj])); 42var_dump(!empty($map[$obj])); 43try { 44 var_dump($map[$obj]); 45} catch (Error $e) { 46 echo $e->getMessage(), "\n"; 47} 48 49// It's okay to unset an object that's not in the map. 50unset($map[new stdClass]); 51 52echo "\nIndirect modification:\n"; 53$map[$obj] = []; 54$map[$obj][] = 42; 55$map[$obj2] = 41; 56$map[$obj2]++; 57var_dump($map); 58 59echo "\nMethods:\n"; 60var_dump($map->offsetSet($obj2, 43)); 61var_dump($map->offsetGet($obj2)); 62var_dump($map->offsetExists($obj2)); 63var_dump($map->count()); 64var_dump($map->offsetUnset($obj2)); 65var_dump($map->count()); 66 67?> 68--EXPECT-- 69int(0) 70int(1) 71object(WeakMap)#1 (1) { 72 [0]=> 73 array(2) { 74 ["key"]=> 75 object(stdClass)#2 (1) { 76 ["value"]=> 77 int(1) 78 } 79 ["value"]=> 80 object(stdClass)#3 (1) { 81 ["value"]=> 82 int(2) 83 } 84 } 85} 86bool(true) 87bool(true) 88object(stdClass)#3 (1) { 89 ["value"]=> 90 int(2) 91} 92object(WeakMap)#1 (1) { 93 [0]=> 94 array(2) { 95 ["key"]=> 96 object(stdClass)#2 (1) { 97 ["value"]=> 98 int(1) 99 } 100 ["value"]=> 101 int(42) 102 } 103} 104bool(true) 105bool(true) 106int(42) 107object(WeakMap)#1 (1) { 108 [0]=> 109 array(2) { 110 ["key"]=> 111 object(stdClass)#2 (1) { 112 ["value"]=> 113 int(1) 114 } 115 ["value"]=> 116 bool(false) 117 } 118} 119bool(true) 120bool(false) 121bool(false) 122object(WeakMap)#1 (1) { 123 [0]=> 124 array(2) { 125 ["key"]=> 126 object(stdClass)#2 (1) { 127 ["value"]=> 128 int(1) 129 } 130 ["value"]=> 131 NULL 132 } 133} 134bool(false) 135bool(false) 136NULL 137object(WeakMap)#1 (0) { 138} 139bool(false) 140bool(false) 141Object stdClass#2 not contained in WeakMap 142 143Indirect modification: 144object(WeakMap)#1 (2) { 145 [0]=> 146 array(2) { 147 ["key"]=> 148 object(stdClass)#2 (1) { 149 ["value"]=> 150 int(1) 151 } 152 ["value"]=> 153 array(1) { 154 [0]=> 155 int(42) 156 } 157 } 158 [1]=> 159 array(2) { 160 ["key"]=> 161 object(stdClass)#3 (1) { 162 ["value"]=> 163 int(2) 164 } 165 ["value"]=> 166 int(42) 167 } 168} 169 170Methods: 171NULL 172int(43) 173bool(true) 174int(2) 175NULL 176int(1) 177