xref: /PHP-8.1/ext/spl/tests/fixedarray_001.phpt (revision e9e06279)
1--TEST--
2SPL: FixedArray: std operations
3--FILE--
4<?php
5$a = new SplFixedArray(0);
6// errors
7try {
8    $a[0] = "value1";
9} catch (RuntimeException $e) {
10    echo $e::class, ': ', $e->getMessage(), "\n";
11}
12try {
13    var_dump($a["asdf"]);
14} catch (\TypeError $e) {
15    echo $e::class, ': ', $e->getMessage(), "\n";
16}
17try {
18    unset($a[-1]);
19} catch (RuntimeException $e) {
20    echo $e::class, ': ', $e->getMessage(), "\n";
21}
22$a->setSize(10);
23
24
25$a[0] = "value0";
26$a[1] = "value1";
27$a[2] = "value2";
28$a[3] = "value3";
29$ref = "value4";
30$ref2 =&$ref;
31$a[4] = $ref;
32$ref = "value5";
33
34unset($a[1]);
35
36var_dump($a[0], $a[2], $a[3], $a[4]);
37
38// countable
39
40var_dump(count($a), $a->getSize(), count($a) == $a->getSize());
41
42// clonable
43$b = clone $a;
44$a[0] = "valueNew";
45var_dump($b[0]);
46?>
47--EXPECT--
48RuntimeException: Index invalid or out of range
49TypeError: Illegal offset type
50RuntimeException: Index invalid or out of range
51string(6) "value0"
52string(6) "value2"
53string(6) "value3"
54string(6) "value4"
55int(10)
56int(10)
57bool(true)
58string(6) "value0"
59