|
| 1 | +--TEST-- |
| 2 | +Named parameters define order |
| 3 | +--FILE-- |
| 4 | +<?php |
| 5 | + |
| 6 | +function f($a, $b, $c, $d = null) { |
| 7 | + return [$a, $b, $c, $d]; |
| 8 | +} |
| 9 | + |
| 10 | +function g($a, $b, $c, ...$d) { |
| 11 | + return [$a, $b, $c, $d]; |
| 12 | +} |
| 13 | + |
| 14 | +echo "# All named\n"; |
| 15 | + |
| 16 | +$a = f(d: ?, c: ?, b: ?, a: ?); |
| 17 | +echo new ReflectionFunction($a), "\n"; |
| 18 | +var_dump($a(1, 2, 3, 4)); |
| 19 | + |
| 20 | +echo "# Some named: Positional first, then named in specified order\n"; |
| 21 | + |
| 22 | +$a = f(?, ?, d: ?, c: ?); |
| 23 | +echo new ReflectionFunction($a), "\n"; |
| 24 | +var_dump($a(1, 2, 3, 4)); |
| 25 | + |
| 26 | +echo "# Some named, one unspecified\n"; |
| 27 | + |
| 28 | +$a = f(?, c: ?, b: ?); |
| 29 | +echo new ReflectionFunction($a), "\n"; |
| 30 | +var_dump($a(1, 2, 3, 4)); |
| 31 | + |
| 32 | +echo "# Some named, some implicit added by '...'\n"; |
| 33 | + |
| 34 | +$a = f(c: ?, b: ?, ...); |
| 35 | +echo new ReflectionFunction($a), "\n"; |
| 36 | +var_dump($a(1, 2, 3, 4)); |
| 37 | + |
| 38 | +echo "# Some named, some implicit added by '...' on variadic function\n"; |
| 39 | + |
| 40 | +$a = g(c: ?, b: ?, ...); |
| 41 | +echo new ReflectionFunction($a), "\n"; |
| 42 | +var_dump($a(1, 2, 3, 4, 5, 6)); |
| 43 | + |
| 44 | +echo "# Some prebound, some named\n"; |
| 45 | + |
| 46 | +$a = f(-1, c: ?, d: -2, b: ?); |
| 47 | +echo new ReflectionFunction($a), "\n"; |
| 48 | +var_dump($a(1, 2)); |
| 49 | + |
| 50 | +echo "# Some named, some required missing\n"; |
| 51 | + |
| 52 | +try { |
| 53 | + $a = f(b: ?, c: ?); |
| 54 | +} catch (Error $e) { |
| 55 | + echo $e::class, ": ", $e->getMessage(), "\n"; |
| 56 | +} |
| 57 | + |
| 58 | +?> |
| 59 | +--EXPECTF-- |
| 60 | +# All named |
| 61 | +Closure [ <user> static function {closure:%s:%d} ] { |
| 62 | + @@ %sparam_reorder.php 13 - 13 |
| 63 | + |
| 64 | + - Parameters [4] { |
| 65 | + Parameter #0 [ <required> $d ] |
| 66 | + Parameter #1 [ <required> $c ] |
| 67 | + Parameter #2 [ <required> $b ] |
| 68 | + Parameter #3 [ <required> $a ] |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +array(4) { |
| 73 | + [0]=> |
| 74 | + int(4) |
| 75 | + [1]=> |
| 76 | + int(3) |
| 77 | + [2]=> |
| 78 | + int(2) |
| 79 | + [3]=> |
| 80 | + int(1) |
| 81 | +} |
| 82 | +# Some named: Positional first, then named in specified order |
| 83 | +Closure [ <user> static function {closure:%s:%d} ] { |
| 84 | + @@ %sparam_reorder.php 19 - 19 |
| 85 | + |
| 86 | + - Parameters [4] { |
| 87 | + Parameter #0 [ <required> $a ] |
| 88 | + Parameter #1 [ <required> $b ] |
| 89 | + Parameter #2 [ <required> $d ] |
| 90 | + Parameter #3 [ <required> $c ] |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +array(4) { |
| 95 | + [0]=> |
| 96 | + int(1) |
| 97 | + [1]=> |
| 98 | + int(2) |
| 99 | + [2]=> |
| 100 | + int(4) |
| 101 | + [3]=> |
| 102 | + int(3) |
| 103 | +} |
| 104 | +# Some named, one unspecified |
| 105 | +Closure [ <user> static function {closure:%s:%d} ] { |
| 106 | + @@ %sparam_reorder.php 25 - 25 |
| 107 | + |
| 108 | + - Parameters [3] { |
| 109 | + Parameter #0 [ <required> $a ] |
| 110 | + Parameter #1 [ <required> $c ] |
| 111 | + Parameter #2 [ <required> $b ] |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +array(4) { |
| 116 | + [0]=> |
| 117 | + int(1) |
| 118 | + [1]=> |
| 119 | + int(3) |
| 120 | + [2]=> |
| 121 | + int(2) |
| 122 | + [3]=> |
| 123 | + NULL |
| 124 | +} |
| 125 | +# Some named, some implicit added by '...' |
| 126 | +Closure [ <user> static function {closure:%s:%d} ] { |
| 127 | + @@ %sparam_reorder.php 31 - 31 |
| 128 | + |
| 129 | + - Parameters [4] { |
| 130 | + Parameter #0 [ <required> $c ] |
| 131 | + Parameter #1 [ <required> $b ] |
| 132 | + Parameter #2 [ <required> $a ] |
| 133 | + Parameter #3 [ <optional> $d = NULL ] |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +array(4) { |
| 138 | + [0]=> |
| 139 | + int(3) |
| 140 | + [1]=> |
| 141 | + int(2) |
| 142 | + [2]=> |
| 143 | + int(1) |
| 144 | + [3]=> |
| 145 | + int(4) |
| 146 | +} |
| 147 | +# Some named, some implicit added by '...' on variadic function |
| 148 | +Closure [ <user> static function {closure:%s:%d} ] { |
| 149 | + @@ %sparam_reorder.php 37 - 37 |
| 150 | + |
| 151 | + - Parameters [4] { |
| 152 | + Parameter #0 [ <required> $c ] |
| 153 | + Parameter #1 [ <required> $b ] |
| 154 | + Parameter #2 [ <required> $a ] |
| 155 | + Parameter #3 [ <optional> ...$d ] |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +array(4) { |
| 160 | + [0]=> |
| 161 | + int(3) |
| 162 | + [1]=> |
| 163 | + int(2) |
| 164 | + [2]=> |
| 165 | + int(1) |
| 166 | + [3]=> |
| 167 | + array(3) { |
| 168 | + [0]=> |
| 169 | + int(4) |
| 170 | + [1]=> |
| 171 | + int(5) |
| 172 | + [2]=> |
| 173 | + int(6) |
| 174 | + } |
| 175 | +} |
| 176 | +# Some prebound, some named |
| 177 | +Closure [ <user> static function {closure:%s:%d} ] { |
| 178 | + @@ %sparam_reorder.php 43 - 43 |
| 179 | + |
| 180 | + - Bound Variables [2] { |
| 181 | + Variable #0 [ $a ] |
| 182 | + Variable #1 [ $d ] |
| 183 | + } |
| 184 | + |
| 185 | + - Parameters [2] { |
| 186 | + Parameter #0 [ <required> $c ] |
| 187 | + Parameter #1 [ <required> $b ] |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +array(4) { |
| 192 | + [0]=> |
| 193 | + int(-1) |
| 194 | + [1]=> |
| 195 | + int(2) |
| 196 | + [2]=> |
| 197 | + int(1) |
| 198 | + [3]=> |
| 199 | + int(-2) |
| 200 | +} |
| 201 | +# Some named, some required missing |
| 202 | +ArgumentCountError: main(): Argument #1 not passed |
0 commit comments