xref: /PHP-7.1/ext/spl/tests/fixedarray_002.phpt (revision 35026062)
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() {
10        return 2;
11    }
12
13    public function offsetGet($n) {
14        echo "A::offsetGet\n";
15        return parent::offsetGet($n);
16    }
17    public function offsetSet($n, $v) {
18        echo "A::offsetSet\n";
19        return parent::offsetSet($n, $v);
20    }
21    public function offsetUnset($n) {
22        echo "A::offsetUnset\n";
23        return parent::offsetUnset($n);
24    }
25    public function offsetExists($n) {
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 "Exception: ".$e->getMessage()."\n";
38}
39try {
40    var_dump($a["asdf"]);
41} catch (RuntimeException $e) {
42    echo "Exception: ".$e->getMessage()."\n";
43}
44try {
45    unset($a[-1]);
46} catch (RuntimeException $e) {
47    echo "Exception: ".$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===DONE===
71--EXPECTF--
72A::offsetSet
73Exception: Index invalid or out of range
74A::offsetGet
75Exception: Index invalid or out of range
76A::offsetUnset
77Exception: Index invalid or out of range
78A::offsetSet
79A::offsetSet
80A::offsetSet
81A::offsetSet
82A::offsetSet
83A::offsetUnset
84A::offsetExists
85A::offsetExists
86A::offsetExists
87A::offsetExists
88bool(false)
89bool(true)
90bool(true)
91bool(false)
92A::offsetGet
93A::offsetGet
94A::offsetGet
95A::offsetGet
96string(6) "value0"
97string(6) "value2"
98string(6) "value3"
99string(6) "value4"
100int(2)
101int(10)
102bool(false)
103===DONE===
104