1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 08:12:21 +01:00
Files
archived-php-src/Zend/tests/named_params/call_user_func_array.phpt
Nikita Popov d92229d8c7 Implement named parameters
From an engine perspective, named parameters mainly add three
concepts:

 * The SEND_* opcodes now accept a CONST op2, which is the
   argument name. For now, it is looked up by linear scan and
   runtime cached.
 * This may leave UNDEF arguments on the stack. To avoid having
   to deal with them in other places, a CHECK_UNDEF_ARGS opcode
   is used to either replace them with defaults, or error.
 * For variadic functions, EX(extra_named_params) are collected
   and need to be freed based on ZEND_CALL_HAS_EXTRA_NAMED_PARAMS.

RFC: https://wiki.php.net/rfc/named_params

Closes GH-5357.
2020-07-31 15:53:36 +02:00

73 lines
1.6 KiB
PHP

--TEST--
Behavior of call_user_func_array() with named parameters
--FILE--
<?php
namespace {
$test = function($a = 'a', $b = 'b', $c = 'c') {
echo "a = $a, b = $b, c = $c\n";
};
$test_variadic = function(...$args) {
var_dump($args);
};
call_user_func_array($test, ['A', 'B']);
call_user_func_array($test, [1 => 'A', 0 => 'B']);
call_user_func_array($test, ['A', 'c' => 'C']);
call_user_func_array($test_variadic, ['A', 'c' => 'C']);
try {
call_user_func_array($test, ['d' => 'D']);
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
call_user_func_array($test, ['c' => 'C', 'A']);
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
echo "\n";
}
namespace Foo {
call_user_func_array($test, ['A', 'B']);
call_user_func_array($test, [1 => 'A', 0 => 'B']);
call_user_func_array($test, ['A', 'c' => 'C']);
call_user_func_array($test_variadic, ['A', 'c' => 'C']);
try {
call_user_func_array($test, ['d' => 'D']);
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
try {
call_user_func_array($test, ['c' => 'C', 'A']);
} catch (\Error $e) {
echo $e->getMessage(), "\n";
}
}
?>
--EXPECT--
a = A, b = B, c = c
a = A, b = B, c = c
a = A, b = b, c = C
array(2) {
[0]=>
string(1) "A"
["c"]=>
string(1) "C"
}
Unknown named parameter $d
Cannot use positional argument after named argument
a = A, b = B, c = c
a = A, b = B, c = c
a = A, b = b, c = C
array(2) {
[0]=>
string(1) "A"
["c"]=>
string(1) "C"
}
Unknown named parameter $d
Cannot use positional argument after named argument