xref: /PHP-8.0/Zend/tests/dereference_001.phpt (revision c48b745f)
1--TEST--
2Testing array dereference
3--FILE--
4<?php
5error_reporting(E_ALL);
6
7function a() {
8    return array(1,array(5));
9}
10var_dump(a()[1][0]); // int(5)
11
12function b() {
13    return array();
14}
15var_dump(b()[0]); // Notice: Undefined array key 0
16
17class foo {
18    public $y = 1;
19
20    public function test() {
21        return array(array(array('foobar')));
22    }
23}
24
25function c() {
26    return array(new foo);
27}
28var_dump(c()[0]->y); // int(1)
29
30function d() {
31    $obj = new foo;
32    return $obj->test();
33}
34var_dump(d()[0][0][0][3]); // string(1) "b"
35
36function e() {
37    $y = 'bar';
38    $x = array('a' => 'foo', 'b' => $y);
39    return $x;
40}
41var_dump(e()['b']); // string(3) "bar"
42
43?>
44--EXPECTF--
45int(5)
46
47Warning: Undefined array key 0 in %s on line %d
48NULL
49int(1)
50string(1) "b"
51string(3) "bar"
52