1--TEST--
2SplObjectStorage::getHash() implementation
3--FILE--
4<?php
5
6class MySplObjectStorage1 extends SplObjectStorage {
7    #[ReturnTypeWillChange]
8    public function getHash($obj) {
9        return 2;
10    }
11}
12
13class MySplObjectStorage2 extends SplObjectStorage {
14    public function getHash($obj): string {
15        throw new Exception("foo");
16        return "asd";
17    }
18}
19
20class MySplObjectStorage3 extends SplObjectStorage {
21    public function getHash($obj): string {
22        return "asd";
23    }
24}
25
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
37foreach ($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}
49
50?>
51--EXPECT--
52Instance as SplObjectStorage
53string(3) "foo"
54string(3) "foo"
55bool(false)
56Instance as MySplObjectStorage1
57TypeError: MySplObjectStorage1::getHash(): Return value must be of type string, int returned
58Instance as MySplObjectStorage2
59Exception: foo
60Instance as MySplObjectStorage3
61string(3) "foo"
62string(3) "foo"
63bool(true)
64