|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-01-15 21:10 UTC] vtamara at pasosdeJesus dot org
Description: ------------ --- From manual page: http://www.php.net/function.call-user-func-array#refsect1- function.call-user-func-array-notes --- The test script under PHP 5.2.17 produces the expected result shown below. However at least with PHP 5.3, it produces the actual result shown. This change in behavior from PHP 5.2 to PHP 5.3 is not documented (it is documented for PHP 5.4). Test script: --------------- function x(&$a) { return array('x'); } $y = 'y'; echo "1\n"; var_dump(x($y)); echo "2\n"; var_dump(call_user_func_array('x', array($y))); Expected result: ---------------- 1 array(1) { [0]=> string(1) "x" } 2 array(1) { [0]=> string(1) "x" } Actual result: -------------- 1 array(1) { [0]=> string(1) "x" } 2 PHP Warning: Parameter 1 to x() expected to be a reference, value given in /home/vtamara/comp/php/callback.php on line 10 NULL PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 20:00:01 2025 UTC |
To be clear this is not a bug. You're simply confusing two different behaviors. What you're describing in your example is not call-time pass-by-reference behavior. What you point out in the documentation does indeed refer to the changes in call-time pass-by-reference, which did in fact change in PHP 5.4.0. Call-time pass-by-reference is when you call the function with your arguments passed to the function by reference (and not when your function prototype requires arguments to be passed by reference). These are two difference behaviors. One is "pass-by-reference" and the other is "call-time pass-by-reference". The difference is that in the latter the references are created at the time you invoke the function (or at the time you "call" on the function). The former creates the references in the function prototype making them always available to the caller. The form of call-time pass-by-reference this note in the manual refers to was deprecated in PHP 5.3, but did not issue an E_DEPRECATED error. It was removed in PHP 5.4, but the E_WARNING error you're referring to is not about call-time pass-by-reference. It's the fact that your function expects a reference and you are passing a value. This is a result of how call_user_func_array has changed from allowing arguments to always be passed by reference to the callback, regardless of what the callback function expects -- to no longer allowing this behavior by default. Here's an example of call-time pass-by-reference behavior: <?php function x($a) { // notice the function does not require a reference return ++$a; } $y = 1; /* Notice we are passing by reference at call-time */ var_dump(x(&$y)); // int(2) var_dump($y); // int(2) /* Notice the &$y -- again this is call-time pass-by-reference */ var_dump( call_user_func_array('x', array(&$y)), // int(3) $y // int(3) ); ?> In PHP >= 5.4.0 the above code would produce a fatal error of: "Fatal error: Call-time pass-by-reference has been removed; If you would like to pass argument by reference, modify the declaration of x() ..." for line 11 where we call var_dump(x(&$y)). As for call_user_func_array(), you would not get a fatal error or a warning, but $y will still show int(1) since nothing is actually referenced here. It's the case that in your test script call_user_func_array actually failed to call your function and the reason why you see NULL as the return value.