Skip to content

Add short closures / arrow functions #3941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Zend/tests/arg_unpack/many_args.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ Argument unpacking with many arguments
--FILE--
<?php

function fn(...$args) {
function f(...$args) {
var_dump(count($args));
}

$array = array_fill(0, 10000, 42);
fn(...$array, ...$array);
f(...$array, ...$array);

?>
--EXPECT--
Expand Down
45 changes: 45 additions & 0 deletions Zend/tests/arrow_functions/001.phpt
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)
13 changes: 13 additions & 0 deletions Zend/tests/arrow_functions/002.phpt
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)
21 changes: 21 additions & 0 deletions Zend/tests/arrow_functions/003.phpt
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
13 changes: 13 additions & 0 deletions Zend/tests/arrow_functions/004.phpt
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)
54 changes: 54 additions & 0 deletions Zend/tests/arrow_functions/005.phpt
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)
44 changes: 44 additions & 0 deletions Zend/tests/arrow_functions/006.phpt
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
14 changes: 14 additions & 0 deletions Zend/tests/arrow_functions/007.phpt
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
28 changes: 28 additions & 0 deletions Zend/tests/arrow_functions/008.phpt
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)
4 changes: 2 additions & 2 deletions Zend/tests/generators/bug65035.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Bug #65035: yield / exit segfault
<?php

function gen() {
fn();
f();
yield;
}

function fn() {
function f() {
exit('Done');
}

Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/grammar/semi_reserved_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Obj
function switch(){ echo __METHOD__, PHP_EOL; }
function yield(){ echo __METHOD__, PHP_EOL; }
function function(){ echo __METHOD__, PHP_EOL; }
function fn(){ echo __METHOD__, PHP_EOL; }
function if(){ echo __METHOD__, PHP_EOL; }
function endswitch(){ echo __METHOD__, PHP_EOL; }
function finally(){ echo __METHOD__, PHP_EOL; }
Expand Down Expand Up @@ -135,6 +136,7 @@ $obj->continue();
$obj->switch();
$obj->yield();
$obj->function();
$obj->fn();
$obj->if();
$obj->endswitch();
$obj->finally();
Expand Down Expand Up @@ -213,6 +215,7 @@ Obj::continue
Obj::switch
Obj::yield
Obj::function
Obj::fn
Obj::if
Obj::endswitch
Obj::finally
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/grammar/semi_reserved_002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Obj
static function switch(){ echo __METHOD__, PHP_EOL; }
static function yield(){ echo __METHOD__, PHP_EOL; }
static function function(){ echo __METHOD__, PHP_EOL; }
static function fn(){ echo __METHOD__, PHP_EOL; }
static function if(){ echo __METHOD__, PHP_EOL; }
static function endswitch(){ echo __METHOD__, PHP_EOL; }
static function finally(){ echo __METHOD__, PHP_EOL; }
Expand Down Expand Up @@ -133,6 +134,7 @@ Obj::continue();
Obj::switch();
Obj::yield();
Obj::function();
Obj::fn();
Obj::if();
Obj::endswitch();
Obj::finally();
Expand Down Expand Up @@ -211,6 +213,7 @@ Obj::continue
Obj::switch
Obj::yield
Obj::function
Obj::fn
Obj::if
Obj::endswitch
Obj::finally
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/grammar/semi_reserved_003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Obj
var $switch = 'switch';
var $yield = 'yield';
var $function = 'function';
var $fn = 'fn';
var $if = 'if';
var $endswitch = 'endswitch';
var $finally = 'finally';
Expand Down Expand Up @@ -136,6 +137,7 @@ echo $obj->continue, PHP_EOL;
echo $obj->switch, PHP_EOL;
echo $obj->yield, PHP_EOL;
echo $obj->function, PHP_EOL;
echo $obj->fn, PHP_EOL;
echo $obj->if, PHP_EOL;
echo $obj->endswitch, PHP_EOL;
echo $obj->finally, PHP_EOL;
Expand Down Expand Up @@ -217,6 +219,7 @@ continue
switch
yield
function
fn
if
endswitch
finally
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/grammar/semi_reserved_004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Obj
static $switch = 'switch';
static $yield = 'yield';
static $function = 'function';
static $fn = 'fn';
static $if = 'if';
static $endswitch = 'endswitch';
static $finally = 'finally';
Expand Down Expand Up @@ -134,6 +135,7 @@ echo Obj::$continue, PHP_EOL;
echo Obj::$switch, PHP_EOL;
echo Obj::$yield, PHP_EOL;
echo Obj::$function, PHP_EOL;
echo Obj::$fn, PHP_EOL;
echo Obj::$if, PHP_EOL;
echo Obj::$endswitch, PHP_EOL;
echo Obj::$finally, PHP_EOL;
Expand Down Expand Up @@ -213,6 +215,7 @@ continue
switch
yield
function
fn
if
endswitch
finally
Expand Down
3 changes: 3 additions & 0 deletions Zend/tests/grammar/semi_reserved_005.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Obj
const SWITCH = 'switch';
const YIELD = 'yield';
const FUNCTION = 'function';
const FN = 'fn';
const IF = 'if';
const ENDSWITCH = 'endswitch';
const FINALLY = 'finally';
Expand Down Expand Up @@ -131,6 +132,7 @@ echo Obj::CONTINUE, PHP_EOL;
echo Obj::SWITCH, PHP_EOL;
echo Obj::YIELD, PHP_EOL;
echo Obj::FUNCTION, PHP_EOL;
echo Obj::FN, PHP_EOL;
echo Obj::IF, PHP_EOL;
echo Obj::ENDSWITCH, PHP_EOL;
echo Obj::FINALLY, PHP_EOL;
Expand Down Expand Up @@ -208,6 +210,7 @@ continue
switch
yield
function
fn
if
endswitch
finally
Expand Down
2 changes: 1 addition & 1 deletion Zend/tests/this_as_lexical_var_error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Cannot use $this as lexical variable
<?php

class Foo {
public function fn() {
public function f() {
return function() use ($this) {};
}
}
Expand Down
Loading