Bug #70379 [Com]: call_user_func_array only accepts explicit references
| From: | patrickallaert@php.net | Date: | Sat, 29 Aug 2015 04:28:35 +0000 |
| Subject: | Bug #70379 [Com]: call_user_func_array only accepts explicit references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195636@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70379&edit=1
ID: 70379
Comment by: patrickallaert@php.net
Reported by: lolautruche at gmail dot com
Summary: call_user_func_array only accepts explicit
references
Status: Assigned
Type: Bug
Package: Unknown/Other Function
Operating System: OSX
PHP Version: 7.0.0RC1
Assigned To: laruence
Block user comment: N
Private report: N
New Comment:
Thinking about this, the behavior of PHP 5.x is strange.
When a function accepts a value by reference, the only check should be to ensure that a variable is
passed, otherwise "Fatal error: Only variables can be passed by reference" should be
raised, e.g.:
function by_ref(&$arg) {
$arg = "changed";
};
by_ref("foo"); // Triggers the fatal error
$x = "orig";
by_ref($x); // Changes $x to "changed"
A function using "value by reference" doesn't mean that a reference should be passed
on but that the local variable to the function should be treated as a reference to the original one.
Hence I think that in ext/standard/tests/general_functions/call_user_func_array_variation_001.phpt,
the second test: "Calling by_ref() with unreferenced argument" should NOT emit the warning
"Warning: Parameter 1 to by_ref() expected to be a reference" either.
Note that in PHP 5, one can call:
call_user_func_array("by_ref", ["test"]);
This is inconsistent as well, "Fatal error: Only variables can be passed by reference"
should have been triggered.
Previous Comments:
------------------------------------------------------------------------
[2015-08-28 09:00:03] lolautruche at gmail dot com
Description:
------------
In PHP 7, passing variables assigned by reference to call_user_func_array() (as part of arguments)
now triggers a warning :
"Warning: Parameter 1 to ... expected to be a reference, value given in..."
It worked fine in PHP 5.6.
Current workaround is to explicitly pass the value by reference (see code).
Not sure if this is on purpose, but I couldn't find any mention in the UPGRADING doc about this
specifically.
Test script:
---------------
<?php
$foo = function (&$arg) {
var_dump($arg);
};
$a = 'bar';
$b =& $a;
// Warning: Parameter 1 to {closure}() expected to be a reference, value given in
/Users/lolautruche/Desktop/reference.php on line 8
call_user_func_array($foo, [$b]);
// Works fine
call_user_func_array($foo, [&$a]);
Expected result:
----------------
string(3) "bar"
string(3) "bar"
Actual result:
--------------
Warning: Parameter 1 to {closure}() expected to be a reference, value given in
/Users/lolautruche/Desktop/reference.php on line 10
string(3) "bar"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70379&edit=1