-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Closure self-reference #11118
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
Open
KapitanOczywisty
wants to merge
3
commits into
php:master
Choose a base branch
from
KapitanOczywisty:closure_self_reference
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[RFC] Closure self-reference #11118
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
--TEST-- | ||
Testing Closure self-reference functionality: Arrow functions | ||
--FILE-- | ||
<?php | ||
|
||
$factorial = fn(int $num) as $fn : int => $num > 1 ? $num * $fn($num - 1) : $num; | ||
|
||
echo "5! = ", $factorial(5), PHP_EOL; | ||
|
||
|
||
$fn = fn() as $a => fn(int $b) as $a => $a; | ||
|
||
echo new ReflectionParameter($fn()(1), 'b'), PHP_EOL; | ||
|
||
|
||
$c = 123; | ||
$fn = fn() as $c => fn() => $c; | ||
|
||
echo get_class($fn()()), PHP_EOL; | ||
|
||
?> | ||
--EXPECTF-- | ||
5! = 120 | ||
Parameter #0 [ <required> int $b ] | ||
Closure |
This file contains hidden or 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,40 @@ | ||
--TEST-- | ||
Testing Closure self-reference functionality: Basic | ||
--FILE-- | ||
<?php | ||
|
||
$factorial = function(int $num) as $factorial { | ||
if($num > 1) { | ||
return $num * $factorial($num - 1); | ||
} | ||
return $num; | ||
}; | ||
|
||
echo "4! = ", $factorial(4), PHP_EOL; | ||
|
||
$dynamic = function(bool $root = false) as $test use(&$dynamic) { | ||
if ($root) { | ||
$dynamic = function() { | ||
return "Hello"; | ||
}; | ||
return $test(); | ||
} else { | ||
return "Hi"; | ||
} | ||
}; | ||
|
||
echo $dynamic(true), PHP_EOL; | ||
echo $dynamic(true), PHP_EOL; | ||
|
||
echo (function() as $fn : string { | ||
echo get_class($fn), PHP_EOL; | ||
return "Bye"; | ||
})(), PHP_EOL; | ||
|
||
?> | ||
--EXPECTF-- | ||
4! = 24 | ||
Hi | ||
Hello | ||
Closure | ||
Bye |
10 changes: 10 additions & 0 deletions
10
Zend/tests/closures/closure_self_reference_error_arrow_param.phpt
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Closure self-reference can't reuse parameter variable name (arrow function) | ||
--FILE-- | ||
<?php | ||
|
||
fn(int $a) as $a => 1; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use lexical variable $a as self reference in %s on line %d |
10 changes: 10 additions & 0 deletions
10
Zend/tests/closures/closure_self_reference_error_arrow_this.phpt
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Closure self-reference can't use this as a variable name (arrow function) | ||
--FILE-- | ||
<?php | ||
|
||
fn() as $this => 1; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use $this as self reference in %s on line %d |
10 changes: 10 additions & 0 deletions
10
Zend/tests/closures/closure_self_reference_error_bind.phpt
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Closure self-reference can't reuse static variable name | ||
--FILE-- | ||
<?php | ||
|
||
function() as $a use($a) {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use lexical variable $a as self reference in %s on line %d |
10 changes: 10 additions & 0 deletions
10
Zend/tests/closures/closure_self_reference_error_param.phpt
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Closure self-reference can't reuse parameter variable name | ||
--FILE-- | ||
<?php | ||
|
||
function(int $a) as $a {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use lexical variable $a as self reference in %s on line %d |
10 changes: 10 additions & 0 deletions
10
Zend/tests/closures/closure_self_reference_error_this.phpt
This file contains hidden or 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,10 @@ | ||
--TEST-- | ||
Closure self-reference can't use this as a variable name | ||
--FILE-- | ||
<?php | ||
|
||
function() as $this {}; | ||
|
||
?> | ||
--EXPECTF-- | ||
Fatal error: Cannot use $this as self reference in %s on line %d |
This file contains hidden or 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,23 @@ | ||
--TEST-- | ||
Testing Closure self-reference functionality: Generator | ||
--FILE-- | ||
<?php | ||
|
||
$fn = function(int $max) as $fn { | ||
yield from range($max, 0, -1); | ||
if ($max > 1) { | ||
yield from $fn($max - 1); | ||
} | ||
}; | ||
print_r(iterator_to_array($fn(2), false)); | ||
|
||
?> | ||
--EXPECT-- | ||
Array | ||
( | ||
[0] => 2 | ||
[1] => 1 | ||
[2] => 0 | ||
[3] => 1 | ||
[4] => 0 | ||
) |
This file contains hidden or 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,26 @@ | ||
--TEST-- | ||
Testing Closure self-reference functionality: Closure instances | ||
--FILE-- | ||
<?php | ||
|
||
$map = new WeakMap(); | ||
|
||
$fn = function() as $fn use ($map) { | ||
var_dump(isset($map[$fn])); | ||
}; | ||
$map[$fn] = 1; | ||
$fn(); | ||
$fn->call(new class {}); | ||
$fn(); | ||
|
||
var_dump(count($map)); | ||
unset($fn); | ||
var_dump(count($map)); | ||
|
||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(false) | ||
bool(true) | ||
int(1) | ||
int(0) |
This file contains hidden or 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,32 @@ | ||
--TEST-- | ||
Testing Closure self-reference functionality: This binding | ||
--FILE-- | ||
<?php | ||
|
||
$closure = null; | ||
$fn = function(bool $root = false, bool $set = false) as $fn use(&$closure) : void { | ||
echo $this::A, PHP_EOL; | ||
if ($set) { | ||
$closure = $fn; | ||
} | ||
if ($root) { | ||
$fn(); | ||
} | ||
return; | ||
}; | ||
$fn = $fn->bindTo(new class{const A="bind";}); | ||
$fn(true, true); | ||
$fn->call(new class{const A="call";}, true, true); | ||
$fn(true, false); | ||
$closure(true, false); | ||
|
||
?> | ||
--EXPECTF-- | ||
bind | ||
bind | ||
call | ||
call | ||
bind | ||
bind | ||
call | ||
call |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.