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