-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Per RFC: https://wiki.php.net/rfc/arrow_functions_v2 Co-authored-by: Levi Morrison <[email protected]> Co-authored-by: Bob Weinand <[email protected]>
- Loading branch information
1 parent
eaab0a2
commit f3e5bbe
Showing
27 changed files
with
465 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--TEST-- | ||
Basic arrow function functionality check | ||
--FILE-- | ||
<?php | ||
|
||
$foo = fn() => 1; | ||
var_dump($foo()); | ||
|
||
$foo = fn($x) => $x; | ||
var_dump($foo(2)); | ||
|
||
$foo = fn($x, $y) => $x + $y; | ||
var_dump($foo(1, 2)); | ||
|
||
// Closing over $var | ||
$var = 4; | ||
$foo = fn() => $var; | ||
var_dump($foo()); | ||
|
||
// Not closing over $var, it's a parameter | ||
$foo = fn($var) => $var; | ||
var_dump($foo(5)); | ||
|
||
// Close over $var by-value, not by-reference | ||
$var = 5; | ||
$foo = fn() => ++$var; | ||
var_dump($foo()); | ||
var_dump($var); | ||
|
||
// Nested arrow functions closing over variable | ||
$var = 6; | ||
var_dump((fn() => fn() => $var)()()); | ||
var_dump((fn() => function() use($var) { return $var; })()()); | ||
|
||
?> | ||
--EXPECT-- | ||
int(1) | ||
int(2) | ||
int(3) | ||
int(4) | ||
int(5) | ||
int(6) | ||
int(5) | ||
int(6) | ||
int(6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
Arrow functions implicit use must be throwing notices only upon actual use | ||
--FILE-- | ||
<?php | ||
|
||
$b = 1; | ||
|
||
var_dump((fn() => $b + $c)()); | ||
|
||
?> | ||
--EXPECTF-- | ||
Notice: Undefined variable: c in %s on line %d | ||
int(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Variable-variables inside arrow functions | ||
--FILE-- | ||
<?php | ||
|
||
$a = 1; | ||
$var = "a"; | ||
$fn = fn() => $$var; | ||
var_dump($fn()); | ||
|
||
${5} = 2; | ||
$fn = fn() => ${5}; | ||
var_dump($fn()); | ||
|
||
?> | ||
--EXPECTF-- | ||
Notice: Undefined variable: a in %s on line %d | ||
NULL | ||
|
||
Notice: Undefined variable: 5 in %s on line %d | ||
NULL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--TEST-- | ||
Auto-globals in arrow functions | ||
--FILE-- | ||
<?php | ||
|
||
// This should work, but *not* generate a binding for $GLOBALS | ||
$a = 123; | ||
$fn = fn() => $GLOBALS['a']; | ||
var_dump($fn()); | ||
|
||
?> | ||
--EXPECT-- | ||
int(123) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--TEST-- | ||
Arrow function $this binding | ||
--FILE-- | ||
<?php | ||
|
||
class Test { | ||
public function method() { | ||
// It would be okay if this is NULL, but the rest should work | ||
$fn = fn() => 42; | ||
$r = new ReflectionFunction($fn); | ||
var_dump($r->getClosureThis()); | ||
|
||
$fn = fn() => $this; | ||
var_dump($fn()); | ||
|
||
$fn = fn() => Test::method2(); | ||
$fn(); | ||
|
||
$fn = fn() => call_user_func('Test::method2'); | ||
$fn(); | ||
|
||
$thisName = "this"; | ||
$fn = fn() => $$thisName; | ||
var_dump($fn()); | ||
|
||
$fn = fn() => self::class; | ||
var_dump($fn()); | ||
|
||
// static can be used to unbind $this | ||
$fn = static fn() => isset($this); | ||
var_dump($fn()); | ||
} | ||
|
||
public function method2() { | ||
var_dump($this); | ||
} | ||
} | ||
|
||
(new Test)->method(); | ||
|
||
?> | ||
--EXPECT-- | ||
object(Test)#1 (0) { | ||
} | ||
object(Test)#1 (0) { | ||
} | ||
object(Test)#1 (0) { | ||
} | ||
object(Test)#1 (0) { | ||
} | ||
object(Test)#1 (0) { | ||
} | ||
string(4) "Test" | ||
bool(false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--TEST-- | ||
Arrow functions syntax variations | ||
--FILE-- | ||
<?php | ||
|
||
// By-reference argument and return | ||
$var = 1; | ||
$id = fn&(&$x) => $x; | ||
$ref =& $id($var); | ||
$ref++; | ||
var_dump($var); | ||
|
||
// int argument and return type | ||
$var = 10; | ||
$int_fn = fn(int $x): int => $x; | ||
var_dump($int_fn($var)); | ||
try { | ||
$int_fn("foo"); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
$varargs = fn(?int... $args): array => $args; | ||
var_dump($varargs(20, null, 30)); | ||
try { | ||
$varargs(40, "foo"); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--EXPECTF-- | ||
int(2) | ||
int(10) | ||
Argument 1 passed to {closure}() must be of the type int, string given, called in %s on line %d | ||
array(3) { | ||
[0]=> | ||
int(20) | ||
[1]=> | ||
NULL | ||
[2]=> | ||
int(30) | ||
} | ||
Argument 2 passed to {closure}() must be of the type int or null, string given, called in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--TEST-- | ||
Pretty printing for arrow functions | ||
--FILE-- | ||
<?php | ||
|
||
// TODO We're missing parentheses for the direct call | ||
assert((fn() => false)()); | ||
assert((fn&(int... $args): ?bool => $args[0])(false)); | ||
|
||
?> | ||
--EXPECTF-- | ||
Warning: assert(): assert(fn() => false()) failed in %s on line %d | ||
|
||
Warning: assert(): assert(fn&(int ...$args): ?bool => $args[0](false)) failed in %s on line %d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--TEST-- | ||
Yield inside arrow functions | ||
--FILE-- | ||
<?php | ||
|
||
// This doesn't make terribly much sense, but it works... | ||
|
||
$fn = fn() => yield 123; | ||
foreach ($fn() as $val) { | ||
var_dump($val); | ||
} | ||
|
||
$fn = fn() => yield from [456, 789]; | ||
foreach ($fn() as $val) { | ||
var_dump($val); | ||
} | ||
|
||
$fn = fn() => fn() => yield 987; | ||
foreach ($fn()() as $val) { | ||
var_dump($val); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
int(123) | ||
int(456) | ||
int(789) | ||
int(987) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.