Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does PHP's call_user_func() function not support passing by reference?
    text
    copied!<p>Why don't the function handling functions like <code>call_user_func()</code> support passing parameters by reference?</p> <p>The <a href="http://php.net/call_user_func">docs</a> say terse things like "Note that the parameters for call_user_func() are not passed by reference." I assume the PHP devs had some kind of reason for disabling that capability in this case.</p> <p>Were they facing a technical limitation? Was it a language design choice? How did this come about?</p> <p>EDIT:</p> <p>In order to clarify this, here is an example.</p> <pre><code>&lt;?php function more(&amp;$var){ $var++; } $count = 0; print "The count is $count.\n"; more($count); print "The count is $count.\n"; call_user_func('more', $count); print "The count is $count.\n"; // Output: // The count is 0. // The count is 1. // The count is 1. </code></pre> <p>This is functioning normally; call_user_func does not pass $count by reference, even though more() declared it as a referenced variable. The <a href="http://php.net/call_user_func">call_user_func documentation</a> clearly says that this is the way it's supposed to work.</p> <p>I am well aware that I can get the effect I need by using <code>call_user_func_array('more', array(&amp;$count))</code>.</p> <p>The question is: why was call_user_func <strong>designed</strong> to work this way? The <a href="http://php.net/manual/en/language.references.pass.php">passing by reference documentation</a> says that "Function definitions alone are enough to correctly pass the argument by reference." The behavior of call_user_func is an exception to that. Why?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload