1--TEST-- 2SplObjectStorage magic operators 3--FILE-- 4<?php 5$i = 'dynamic'; 6$v = $i . '_'; 7$s = new SplObjectStorage(); 8$o1 = new Stdclass; 9$o2 = new Stdclass; 10var_dump($s[$o1] ?? 'default'); 11var_dump($s[$o1] ??= $i); 12var_dump($s[$o1] ??= null); 13var_dump($s[$o1] ??= false); 14var_dump($s[$o1] ?? $i); 15var_dump(isset($s[$o1])); 16var_dump(empty($s[$o1])); 17echo "o2\n"; 18var_dump(isset($s[$o2])); 19var_dump(empty($s[$o2])); 20$s[$o2] = null; 21var_dump($s[$o2] ?? new stdClass()); 22echo "check isset/empty/contains for null. offsetExists returns true as long as the entry is there.\n"; 23var_dump(isset($s[$o2])); 24var_dump(empty($s[$o2])); 25var_dump($s->contains($o2)); 26echo "check isset/empty/contains for false.\n"; 27$s[$o2] = false; 28var_dump(isset($s[$o2])); 29var_dump(empty($s[$o2])); 30var_dump($s->contains($o2)); 31try { 32 $s['invalid'] = 123; 33} catch (Error $e) { 34 printf("%s: %s\n", $e::class, $e->getMessage()); 35} 36try { 37 var_dump(isset($s['invalid'])); 38} catch (Error $e) { 39 printf("%s: %s\n", $e::class, $e->getMessage()); 40} 41$a = &$s[$o1]; 42 43var_dump($s); 44 45?> 46--EXPECTF-- 47string(7) "default" 48string(7) "dynamic" 49string(7) "dynamic" 50string(7) "dynamic" 51string(7) "dynamic" 52bool(true) 53bool(false) 54o2 55bool(false) 56bool(true) 57object(stdClass)#4 (0) { 58} 59check isset/empty/contains for null. offsetExists returns true as long as the entry is there. 60bool(true) 61bool(true) 62bool(true) 63check isset/empty/contains for false. 64bool(true) 65bool(true) 66bool(true) 67TypeError: SplObjectStorage::offsetSet(): Argument #1 ($object) must be of type object, string given 68TypeError: SplObjectStorage::offsetExists(): Argument #1 ($object) must be of type object, string given 69 70Notice: Indirect modification of overloaded element of SplObjectStorage has no effect in %s on line 38 71object(SplObjectStorage)#1 (1) { 72 ["storage":"SplObjectStorage":private]=> 73 array(2) { 74 [0]=> 75 array(2) { 76 ["obj"]=> 77 object(stdClass)#2 (0) { 78 } 79 ["inf"]=> 80 string(7) "dynamic" 81 } 82 [1]=> 83 array(2) { 84 ["obj"]=> 85 object(stdClass)#3 (0) { 86 } 87 ["inf"]=> 88 bool(false) 89 } 90 } 91}