|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-06-03 10:47 UTC] contact at jaborandi dot org dot ru
Description:
------------
calling Closure::call() method on anonymouse function that accepts arguments by reference triggers Warning and doesn't execute the function
if same closure is called in regular way, or using call_user_func, or if it does not accept any argument by reference, it is executed normally
tested in 7.0.7, as well as in 7.0.3, 7.0.4, 7.0.5, with the same result
(haven't tested call_user_func in 7.0.5 though, sandbox disabled it)
Test script:
---------------
$a = function(&$arg) { echo "ok"; };
$z = 1;
class MyContext {}
$a->call(new MyContext, $z);
Expected result:
----------------
ok
Actual result:
--------------
Warning: Parameter 1 to MyContext::{closure}() expected to be a reference, value given in C:\bin\php7.0.7\bug.php on line 6
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 05:00:01 2025 UTC |
Extra test code, to compare with cases when works: $a = function($arg) { echo "\nok"; }; $aRef = function(&$arg) { echo "\nok"; }; $z = 1; $a($z); // ok $aRef($z); // ok call_user_func($a, $z); // ok call_user_func($aRef, $z); // ok class MyContext {} $a->call(new MyContext, $z); // ok $aRef->call(new MyContext, $z); // WARNING ... Expected: ok ok ok ok ok ok Actual: ok ok ok ok ok Warning: Parameter 1 to MyContext::{closure}() expected to be a reference, value given in C:\bin\php7.0.7\bug.php on line 17A misinformation in my original post, call_user_func does not (always) work. It works only if function was called in a regular way first (something having to do with internal caching I guess?), which was done in my test. Here's a corrected sample code: --------------------- $a = function(&$arg) { echo "\nok"; if (isset($this)) echo ', '.get_class($this); echo "\n"; }; $x = 1; $y = 1; $a($x); // ok call_user_func($a, $x); // ok - because $a($x) was previously called call_user_func($a, $y); // WARNING - because $a($y) was not called class MyContext {} $a->call(new MyContext, $x); // WARNING $a = Closure::bind($a, new MyContext); // workaround $a($y); // ok, and in desired context