xref: /php-src/ext/spl/tests/fixedarray_002.phpt (revision 009d48da)
1--TEST--
2SPL: FixedArray: overloading
3--FILE--
4<?php
5class A extends SplFixedArray {
6    public $prop1 = NULL;
7    public $prop2 = NULL;
8
9    public function count(): int {
10        return 2;
11    }
12
13    public function offsetGet($n): mixed {
14        echo "A::offsetGet\n";
15        return parent::offsetGet($n);
16    }
17    public function offsetSet($n, $v): void {
18        echo "A::offsetSet\n";
19        parent::offsetSet($n, $v);
20    }
21    public function offsetUnset($n): void {
22        echo "A::offsetUnset\n";
23        parent::offsetUnset($n);
24    }
25    public function offsetExists($n): bool {
26        echo "A::offsetExists\n";
27        return parent::offsetExists($n);
28    }
29}
30
31$a = new A;
32
33// errors
34try {
35    $a[0] = "value1";
36} catch (RuntimeException $e) {
37    echo $e::class, ': ', $e->getMessage(), "\n";
38}
39try {
40    var_dump($a["asdf"]);
41} catch (\TypeError $e) {
42    echo $e::class, ': ', $e->getMessage(), "\n";
43}
44try {
45    unset($a[-1]);
46} catch (RuntimeException $e) {
47    echo $e::class, ': ', $e->getMessage(), "\n";
48}
49$a->setSize(10);
50
51
52$a[0] = "value0";
53$a[1] = "value1";
54$a[2] = "value2";
55$a[3] = "value3";
56$ref = "value4";
57$ref2 =&$ref;
58$a[4] = $ref;
59$ref = "value5";
60
61unset($a[1]);
62var_dump(isset($a[1]), isset($a[2]), empty($a[1]), empty($a[2]));
63
64var_dump($a[0], $a[2], $a[3], $a[4]);
65
66// countable
67
68var_dump(count($a), $a->getSize(), count($a) == $a->getSize());
69?>
70--EXPECT--
71A::offsetSet
72OutOfBoundsException: Index invalid or out of range
73A::offsetGet
74TypeError: Cannot access offset of type string on SplFixedArray
75A::offsetUnset
76OutOfBoundsException: Index invalid or out of range
77A::offsetSet
78A::offsetSet
79A::offsetSet
80A::offsetSet
81A::offsetSet
82A::offsetUnset
83A::offsetExists
84A::offsetExists
85A::offsetExists
86A::offsetExists
87bool(false)
88bool(true)
89bool(true)
90bool(false)
91A::offsetGet
92A::offsetGet
93A::offsetGet
94A::offsetGet
95string(6) "value0"
96string(6) "value2"
97string(6) "value3"
98string(6) "value4"
99int(2)
100int(10)
101bool(false)
102