|
1 | 1 | --TEST-- |
2 | | -SplObjectStorage::getHash implementation |
| 2 | +SplObjectStorage::getHash() implementation |
3 | 3 | --FILE-- |
4 | 4 | <?php |
5 | | -$s = new SplObjectStorage(); |
6 | | -$o1 = new Stdclass; |
7 | | -$o2 = new Stdclass; |
8 | | -$s[$o1] = "some_value\n"; |
9 | | -echo $s->offsetGet($o1); |
10 | 5 |
|
11 | | -class MySplObjectStorage extends SplObjectStorage { |
| 6 | +class MySplObjectStorage1 extends SplObjectStorage { |
12 | 7 | #[ReturnTypeWillChange] |
13 | 8 | public function getHash($obj) { |
14 | 9 | return 2; |
15 | 10 | } |
16 | 11 | } |
17 | 12 |
|
18 | | -try { |
19 | | - $s1 = new MySplObjectStorage; |
20 | | - $s1[$o1] = "foo"; |
21 | | -} catch(Exception $e) { |
22 | | - echo "caught 1\n"; |
23 | | -} |
24 | | - |
25 | 13 | class MySplObjectStorage2 extends SplObjectStorage { |
26 | 14 | public function getHash($obj): string { |
27 | 15 | throw new Exception("foo"); |
28 | 16 | return "asd"; |
29 | 17 | } |
30 | 18 | } |
31 | 19 |
|
32 | | -try { |
33 | | - $s2 = new MySplObjectStorage2; |
34 | | - $s2[$o2] = "foo"; |
35 | | -} catch(Exception $e) { |
36 | | - echo "caught 2\n"; |
37 | | -} |
38 | | - |
39 | 20 | class MySplObjectStorage3 extends SplObjectStorage { |
40 | 21 | public function getHash($obj): string { |
41 | 22 | return "asd"; |
42 | 23 | } |
43 | 24 | } |
44 | 25 |
|
45 | | -$s3 = new MySplObjectStorage3; |
46 | | -$s3[$o1] = $o1; |
47 | | -var_dump($s3[$o1]); |
48 | | -$s3[$o2] = $o2; |
49 | | - |
50 | | -var_dump($s3[$o1] === $s3[$o2]); |
| 26 | +$s = new SplObjectStorage(); |
| 27 | +$o1 = new stdClass(); |
| 28 | +$o2 = new stdClass(); |
| 29 | + |
| 30 | +$instances = [ |
| 31 | + new SplObjectStorage(), |
| 32 | + new MySplObjectStorage1(), |
| 33 | + new MySplObjectStorage2(), |
| 34 | + new MySplObjectStorage3(), |
| 35 | +]; |
| 36 | + |
| 37 | +foreach ($instances as $instance) { |
| 38 | + echo 'Instance as ', $instance::class, PHP_EOL; |
| 39 | + try { |
| 40 | + $instance[$o1] = 'foo'; |
| 41 | + var_dump($instance->offsetGet($o1)); |
| 42 | + var_dump($instance[$o1]); |
| 43 | + $instance[$o2] = $o2; |
| 44 | + var_dump($instance[$o1] === $instance[$o2]); |
| 45 | + } catch(Throwable $e) { |
| 46 | + echo $e::class, ': ', $e->getMessage(), PHP_EOL; |
| 47 | + } |
| 48 | +} |
51 | 49 |
|
52 | 50 | ?> |
53 | 51 | --EXPECT-- |
54 | | -some_value |
55 | | -caught 1 |
56 | | -caught 2 |
57 | | -object(stdClass)#2 (0) { |
58 | | -} |
| 52 | +Instance as SplObjectStorage |
| 53 | +string(3) "foo" |
| 54 | +string(3) "foo" |
| 55 | +bool(false) |
| 56 | +Instance as MySplObjectStorage1 |
| 57 | +TypeError: MySplObjectStorage1::getHash(): Return value must be of type string, int returned |
| 58 | +Instance as MySplObjectStorage2 |
| 59 | +Exception: foo |
| 60 | +Instance as MySplObjectStorage3 |
| 61 | +string(3) "foo" |
| 62 | +string(3) "foo" |
59 | 63 | bool(true) |
0 commit comments