Created
September 20, 2011 09:57
-
-
Save daniula/1228771 to your computer and use it in GitHub Desktop.
call_user_func silent error
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
<?php | |
$var1 = 0; | |
$var2 = 1; | |
function noreference($var1, $var2) { | |
print "noreference\n"; | |
print $var1 + $var2."\n"; | |
} | |
function reference(&$var1, &$var2) { | |
print "reference\n"; | |
print $var1 + $var2."\n"; | |
} | |
call_user_func('noreference', $var1, $var2); | |
call_user_func('reference', $var1, $var2); |
According to this: http://php.net/manual/en/function.call-user-func.php and "Note that the parameters for call_user_func() are not passed by reference." you have to do this that way:
<?php
$var1 = 0;
$var2 = 1;
function noreference($var1, $var2) {
print "noreference\n";
print $var1 + $var2."\n";
}
function reference(&$var1, &$var2) {
print "reference\n";
print $var1 + $var2."\n";
}
call_user_func('noreference', $var1, $var2); //OK
call_user_func('reference', $var1, $var2); //wrong
//pass arguments as an array, instead of call_user_func params
call_user_func_array('reference', array(&$var1, &$var2)); //forced reference
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output under PHP 5.3.4:
1
PHP Warning: Parameter 1 to reference() expected to be a reference, value given in ***/test.php on line 17
Output under PHP 5.3.6:
noreference
1