|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-01-09 09:11 UTC] beerend dot lauwers at capgemini dot com
Description:
------------
Let's say we have a member variable $foo, which will hold something callable.
We then assign to $foo a closure, say, function($x){ return $x; }:
$this->foo = function($x) { return $x; };
Then, we assign to $foo a new closure, which brings into a scope another closure:
$another_f = function($x) { return $x; };
$this->foo = function() use ($another_f) {
$args = func_get_args();
call_user_func_array( $this->foo, $args ); // Crash occurs here
call_user_func_array( $another_f, $args );
};
The PHP process will crash on the line where it calls $this->foo as a function. Probably recurses into an infinite loop.
Test script:
---------------
https://gist.github.com/BeerendLauwers/c28585ce4868ab42d2f9
Expected result:
----------------
The value returned from $t->apply() should return TRUE.
Actual result:
--------------
PHP process crashes. As a workaround, you can assign $this->foo to a placeholder variable and bring it into scope into the closure via use, which will bind it correctly:
$placeholder = $this->foo;
$another_f = function($x) { return $x; };
$this->foo = function() use ($another_f,$placeholder) {
$args = func_get_args();
call_user_func_array( $placeholder, $args );
call_user_func_array( $another_f, $args );
};
Perhaps make it illegal to call a member variable as a function while assigning it to itself, because the value of $this->foo is not captured correctly by the closure.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 11:00:02 2025 UTC |
The issue in the first place is that the function happens to be recursive. This can be shown by this snippet: function a() {call_user_func('a');} a(); This leads to the stack overflow. You should ensure the function would terminate, or avoid passing the function to itself. Thanks.