|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-09-27 19:31 UTC] mryaggi at hotmail dot com
Description:
------------
call_user_func now issue a warning in PHP5.3 when giving a value instead of a
reference.
However, this warning shows up in inappropriate cases.
Test script:
---------------
<?php
//Function to be called via call_user_func
function test(&$z)
{
echo "ok : " . $z . "\n";
}
// - 1 : With a local variable
//This should work, but...
$a = 1;
call_user_func('test',$a);//Warning: Parameter 1 to test() expected to be a reference, value given
// - 2 : Giving a constant
//This should issue a warning but ...
call_user_func('test',2);//works fine. Output "ok : 2"
// - 3 : Base on a parameter
//This should work, but ...
function test3($p=3)
{
call_user_func('test',$p);//Warning: Parameter 1 to test() expected to be a reference, value given
}
test3();
?>
Expected result:
----------------
ok : 1
<b>Warning</b>: Parameter 1 to test() expected to be a reference, value given in
...
ok : 3
Actual result:
--------------
<b>Warning</b>: Parameter 1 to test() expected to be a reference, value given in
...
ok : 2
<br />
<b>Warning</b>: Parameter 1 to test() expected to be a reference, value given in
...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 14:00:01 2025 UTC |
Thank you for your quick reply. You say : "Use call_user_func_array()." But you can run the test script with call_user_func_array() instad, and still you will get those warnings. My point is : when you call call_user_func you have no idea whether the function called expects references or not (values). The only solution I see is to always give references to call_user_func*() For ex: ----------------- function MY_call_user_func_array($Func,$Args) { foreach(array_keys($Args) as $i) { $Args[$i] =& $Args[$i]; }//make it a ref call_user_func_array($Func,$Args); } function test(&$z){ echo "ok : " . $z . "\n";} $a = 1; MY_call_user_func_array('test',array($a)); ----------------- And here we are! IT WORKS! but it I feel like I just learnt how to poo in PHP...Ok, I'm going to keep it simple. You say "[I] have to use call_user_func_array instead". Please show me how. Note : I'm just trying to avoid the warning properly. This would be quite a solution : call_user_func('ExprectRef', & $p); call_user_func_array('ExprectRef',array( & $p)); But it's a pity you have to write & TWICE. One in the signature, one in the call_user_func call. ...it looks so much like the depreciated call-time pass-by-reference. Don't you think?to work around this problem put the reference in the argument array. e.g.: function foo(&$bar) { $bar = 5; } $a = 1; $b = 2; call_user_func('foo', $a); call_user_func_array('foo', array(&$b)); var_dump($a, $b); gives int(1) int(5)The "workaround" from the last comment is not a good idea, because it can not be applied generally. If you use call_user_func_array('foo', array(&$b)) when the function foo takes an argument by copy instead of by reference and modifies it thinking it doesn't matter, $b will still be modified, which is most certainly undesired. Also, the issue is more serious than described in the original post. It affects call_user_func_array as well as call_user_func, and it is not just a warning that is given, but the whole function does not get executed. This is actually a big deal, it can break a lot of programs. A setting to allow execution of the function in this case would be welcome.@georgir: What's the issue you're seeing with call_user_func_args? Your statement > If you use call_user_func_array('foo', array(&$b)) > when the function foo takes an argument by copy > instead of by reference and modifies it thinking > it doesn't matter, $b will still be modified, > which is most certainly undesired. is not true for any supported versions of PHP, as you can easily verify: http://3v4l.org/dWMfe