xref: /PHP-8.1/Zend/tests/arrow_functions/006.phpt (revision b7fe1b66)
1--TEST--
2Arrow functions syntax variations
3--FILE--
4<?php
5
6// By-reference argument and return
7$var = 1;
8$id = fn&(&$x) => $x;
9$ref =& $id($var);
10$ref++;
11var_dump($var);
12
13// int argument and return type
14$var = 10;
15$int_fn = fn(int $x): int => $x;
16var_dump($int_fn($var));
17try {
18    $int_fn("foo");
19} catch (TypeError $e) {
20    echo $e->getMessage(), "\n";
21}
22
23$varargs = fn(?int... $args): array => $args;
24var_dump($varargs(20, null, 30));
25try {
26    $varargs(40, "foo");
27} catch (TypeError $e) {
28    echo $e->getMessage(), "\n";
29}
30
31?>
32--EXPECTF--
33int(2)
34int(10)
35{closure}(): Argument #1 ($x) must be of type int, string given, called in %s on line %d
36array(3) {
37  [0]=>
38  int(20)
39  [1]=>
40  NULL
41  [2]=>
42  int(30)
43}
44{closure}(): Argument #2 must be of type ?int, string given, called in %s on line %d
45