1
0
mirror of https://github.com/php/php-src.git synced 2026-04-14 11:32:11 +02:00
Files
archived-php-src/Zend/tests/named_params/__invoke.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

65 lines
979 B
PHP

--TEST--
Check that __invoke() works with named parameters
--FILE--
<?php
class Test {
public function __invoke($a = 'a', $b = 'b') {
echo "a: $a, b: $b\n";
}
}
class Test2 {
public function __invoke($a = 'a', $b = 'b', ...$rest) {
echo "a: $a, b: $b\n";
var_dump($rest);
}
}
$test = new Test;
$test(b: 'B', a: 'A');
$test(b: 'B');
try {
$test(b: 'B', c: 'C');
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
echo "\n";
$test2 = new Test2;
$test2(b: 'B', a: 'A', c: 'C');
$test2(b: 'B', c: 'C');
echo "\n";
$test3 = function($a = 'a', $b = 'b') {
echo "a: $a, b: $b\n";
};
$test3(b: 'B', a: 'A');
$test3(b: 'B');
try {
$test3(b: 'B', c: 'C');
} catch (Error $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
a: A, b: B
a: a, b: B
Unknown named parameter $c
a: A, b: B
array(1) {
["c"]=>
string(1) "C"
}
a: a, b: B
array(1) {
["c"]=>
string(1) "C"
}
a: A, b: B
a: a, b: B
Unknown named parameter $c