-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix GH-21362: ReflectionMethod::invoke/invokeArgs() rejects different Closure instances #21366
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
Merged
DanielEScherzer
merged 4 commits into
php:master
from
iliaal:fix/gh-21362-reflection-closure-invoke
Mar 8, 2026
+75
−1
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
277d301
Fix GH-21362: ReflectionMethod::invoke/invokeArgs() rejects different…
iliaal 61d4a39
Improve error message for incompatible Closure in ReflectionMethod::i…
iliaal 428198b
Update error message wording per review feedback
iliaal 345eafa
Add NEWS entry for GH-21362 Reflection Closure identity fix
iliaal 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --TEST-- | ||
| GH-21362 (ReflectionMethod::invokeArgs() for Closure::__invoke() accepts objects from different Closures) | ||
| --FILE-- | ||
| <?php | ||
|
|
||
| $c1 = function ($foo, $bar) { | ||
| echo "c1: foo={$foo}, bar={$bar}\n"; | ||
| }; | ||
|
|
||
| $c2 = function ($bar, $foo) { | ||
| echo "c2: foo={$foo}, bar={$bar}\n"; | ||
| }; | ||
|
|
||
| $m = new ReflectionMethod($c1, '__invoke'); | ||
|
|
||
| // invokeArgs with the correct Closure should work | ||
| $m->invokeArgs($c1, ['foo' => 'FOO', 'bar' => 'BAR']); | ||
|
|
||
| // invokeArgs with a different Closure should throw | ||
| try { | ||
| $m->invokeArgs($c2, ['foo' => 'FOO', 'bar' => 'BAR']); | ||
| echo "No exception thrown\n"; | ||
| } catch (ReflectionException $e) { | ||
| echo $e->getMessage() . "\n"; | ||
| } | ||
|
|
||
| // invoke with a different Closure should also throw | ||
| try { | ||
| $m->invoke($c2, 'FOO', 'BAR'); | ||
| echo "No exception thrown\n"; | ||
| } catch (ReflectionException $e) { | ||
| echo $e->getMessage() . "\n"; | ||
| } | ||
|
|
||
| // Closures from the same source (e.g. loop) share the same op_array | ||
| // and should be allowed to invoke each other's ReflectionMethod | ||
| $closures = []; | ||
| for ($i = 0; $i < 3; $i++) { | ||
| $closures[] = function () use ($i) { return $i; }; | ||
| } | ||
|
|
||
| $m2 = new ReflectionMethod($closures[0], '__invoke'); | ||
| foreach ($closures as $closure) { | ||
| var_dump($m2->invoke($closure)); | ||
| } | ||
| ?> | ||
| --EXPECT-- | ||
| c1: foo=FOO, bar=BAR | ||
| Given Closure is not the same as the reflected Closure | ||
| Given Closure is not the same as the reflected Closure | ||
| int(0) | ||
| int(1) | ||
| int(2) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is wrong for internal closures because they don't have an op_array.
Intuitively, the code also seems a bit convoluted, it feels wrong to have to store the object.
Anyway, here is something that shouldn't be allowed most likely, and even if it were allowed then the access to op_array is still wrong (only allowed for userland):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iliaal thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a valid point, I made a follow-up PR #21389 to address this issue (along with corresponding test).