xref: /PHP-8.0/Zend/tests/constant_arrays.phpt (revision 7aacc705)
1--TEST--
2Constant arrays
3--INI--
4zend.enable_gc=1
5--FILE--
6<?php
7
8define('FOOBAR', [1, 2, 3, ['foo' => 'bar']]);
9const FOO_BAR = [1, 2, 3, ['foo' => 'bar']];
10
11$x = FOOBAR;
12$x[0] = 7;
13var_dump($x, FOOBAR);
14
15$x = FOO_BAR;
16$x[0] = 7;
17var_dump($x, FOO_BAR);
18
19// ensure references are removed
20$x = 7;
21$y = [&$x];
22define('QUX', $y);
23$y[0] = 3;
24var_dump($x, $y, QUX);
25
26// ensure objects not allowed in arrays
27try {
28    define('ELEPHPANT', [new StdClass]);
29} catch (TypeError $exception) {
30    echo $exception->getMessage() . "\n";
31}
32
33// ensure recursion doesn't crash
34$recursive = [];
35$recursive[0] = &$recursive;
36
37try {
38    define('RECURSION', $recursive);
39} catch (ValueError $exception) {
40    echo $exception->getMessage() . "\n";
41}
42?>
43--EXPECTF--
44array(4) {
45  [0]=>
46  int(7)
47  [1]=>
48  int(2)
49  [2]=>
50  int(3)
51  [3]=>
52  array(1) {
53    ["foo"]=>
54    string(3) "bar"
55  }
56}
57array(4) {
58  [0]=>
59  int(1)
60  [1]=>
61  int(2)
62  [2]=>
63  int(3)
64  [3]=>
65  array(1) {
66    ["foo"]=>
67    string(3) "bar"
68  }
69}
70array(4) {
71  [0]=>
72  int(7)
73  [1]=>
74  int(2)
75  [2]=>
76  int(3)
77  [3]=>
78  array(1) {
79    ["foo"]=>
80    string(3) "bar"
81  }
82}
83array(4) {
84  [0]=>
85  int(1)
86  [1]=>
87  int(2)
88  [2]=>
89  int(3)
90  [3]=>
91  array(1) {
92    ["foo"]=>
93    string(3) "bar"
94  }
95}
96int(3)
97array(1) {
98  [0]=>
99  &int(3)
100}
101array(1) {
102  [0]=>
103  int(7)
104}
105define(): Argument #2 ($value) cannot be an object, stdClass given
106define(): Argument #2 ($value) cannot be a recursive array
107