xref: /PHP-8.0/Zend/tests/named_params/unpack.phpt (revision d92229d8)
1--TEST--
2Unpacking named parameters
3--FILE--
4<?php
5
6function test($a, $b, $c) {
7    echo "a = $a, b = $b, c = $c\n";
8}
9
10function test2($a = null, &$b = null) {
11    $b++;
12}
13
14test(...['a' => 'a', 'b' => 'b', 'c' => 'c']);
15test(...['c' => 'c', 'b' => 'b', 'a' => 'a']);
16test(...['a', 'b' => 'b', 'c' => 'c']);
17
18try {
19    test(...['a', 'b' => 'b', 'c']);
20} catch (Error $e) {
21    echo $e->getMessage(), "\n";
22}
23
24try {
25    test(...['a', 'a' => 'a']);
26} catch (Error $e) {
27    echo $e->getMessage(), "\n";
28}
29
30$ary = ['b' => 0];
31$ary2 = $ary;
32test2(...$ary);
33var_dump($ary, $ary2);
34
35test(...new ArrayIterator(['a' => 'a', 'b' => 'b', 'c' => 'c']));
36test(...new ArrayIterator(['c' => 'c', 'b' => 'b', 'a' => 'a']));
37test(...new ArrayIterator(['a', 'b' => 'b', 'c' => 'c']));
38
39try {
40    test(...new ArrayIterator(['a', 'b' => 'b', 'c']));
41} catch (Error $e) {
42    echo $e->getMessage(), "\n";
43}
44
45try {
46    test(...new ArrayIterator(['a', 'a' => 'a']));
47} catch (Error $e) {
48    echo $e->getMessage(), "\n";
49}
50
51$ary = ['b' => 0];
52$ary2 = $ary;
53test2(...new ArrayIterator($ary));
54var_dump($ary, $ary2);
55
56?>
57--EXPECTF--
58a = a, b = b, c = c
59a = a, b = b, c = c
60a = a, b = b, c = c
61Cannot use positional argument after named argument during unpacking
62Named parameter $a overwrites previous argument
63array(1) {
64  ["b"]=>
65  int(1)
66}
67array(1) {
68  ["b"]=>
69  int(0)
70}
71a = a, b = b, c = c
72a = a, b = b, c = c
73a = a, b = b, c = c
74Cannot use positional argument after named argument during unpacking
75Named parameter $a overwrites previous argument
76
77Warning: Cannot pass by-reference argument 2 of test2() by unpacking a Traversable, passing by-value instead in %s on line %d
78array(1) {
79  ["b"]=>
80  int(0)
81}
82array(1) {
83  ["b"]=>
84  int(0)
85}
86