xref: /PHP-8.1/Zend/tests/dereference_002.phpt (revision c48b745f)
1--TEST--
2Testing array dereference on method calls
3--FILE--
4<?php
5
6error_reporting(E_ALL);
7
8class  foo {
9    public function bar() {
10        $x = array();
11        $x[] = 3;
12        $x[] = array(1, 5);
13        $x[] = new foo;
14        return $x;
15    }
16}
17
18$foo = new foo;
19
20var_dump($x = $foo->bar()[1]);
21var_dump($foo->bar()[1][1]);
22var_dump($x[0]);
23var_dump($x = $foo->bar()[2]);
24var_dump($x->bar());
25var_dump($x->bar()[0]);
26
27$x = array();
28$x[] = new foo;
29var_dump($x[0]->bar()[2]);
30var_dump($foo->bar()[2]->bar()[1]);
31var_dump($foo->bar()[2]->bar()[2]->bar()[1][0]);
32var_dump($foo->bar()[2]->bar()[2]->bar()[1][0][1]);
33var_dump($foo->bar()[2]->bar()[2]->bar()[4]);
34var_dump($foo->bar()[3]->bar());
35
36?>
37--EXPECTF--
38array(2) {
39  [0]=>
40  int(1)
41  [1]=>
42  int(5)
43}
44int(5)
45int(1)
46object(foo)#2 (0) {
47}
48array(3) {
49  [0]=>
50  int(3)
51  [1]=>
52  array(2) {
53    [0]=>
54    int(1)
55    [1]=>
56    int(5)
57  }
58  [2]=>
59  object(foo)#3 (0) {
60  }
61}
62int(3)
63object(foo)#3 (0) {
64}
65array(2) {
66  [0]=>
67  int(1)
68  [1]=>
69  int(5)
70}
71int(1)
72
73Warning: Trying to access array offset on value of type int in %s on line %d
74NULL
75
76Warning: Undefined array key 4 in %s on line %d
77NULL
78
79Warning: Undefined array key 3 in %s on line %d
80
81Fatal error: Uncaught Error: Call to a member function bar() on null in %s:%d
82Stack trace:
83#0 {main}
84  thrown in %s on line %d
85