Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer is embedded deep down in the way references work in PHP's <em>model</em> - not necessarily the <em>implementation</em>, because that can vary a lot, particularly in the 5.x versions. I'm sure you've heard the lines, they're not like C pointers, or C++ references, etc etc... Basically when a variable is assigned or bound, it can happen in two ways - either by value (in which case the new variable is bound to a new 'box' containing a copy of the old value), or by reference (in which case the new variable is bound to the same value box as the old value). This is true whether we're talking about variables, or function arguments, or cells in arrays.</p> <p>Things start to get a bit hairy when you start passing references into functions - obviously the intent is to be able to modify the original variables. Quite some time ago, call-time pass-by-reference (the ability to pass a reference into a function that wasn't expecting one) got deprecated, because a function that wasn't aware it was dealing with a reference might 'accidentally' modify the input. Taking it to another level, if that function calls a second function, that itself wasn't expecting a reference... then everything ends up getting disconnected. It <em>might</em> work, but it's not guaranteed, and may break in some PHP version.</p> <p>This is where <code>call_user_func()</code> comes in. Suppose you pass a reference into it (and get the associated the call-time pass-by-reference warning). Then your reference gets bound to a new variable - the parameters of <code>call_user_func()</code> itself. Then when your target function is called, its parameters are <em>not</em> bound where you expect. They're not bound to the original parameters at all. They're bound to the local variables that are in the <code>call_user_func()</code> declaration. <code>call_user_func_array()</code> requires caution too. Putting a reference in an array cell could be trouble - since PHP passes that array with "copy-on-write" semantics, you can't be sure if the array won't get modified underneath you, and the copy won't get detached from the original reference.</p> <p>The most insightful explanation I've seen (which helped me get my head around references) was in a comment on the PHP 'passing by reference' manual:</p> <p><a href="http://ca.php.net/manual/en/language.references.pass.php#99549" rel="noreferrer">http://ca.php.net/manual/en/language.references.pass.php#99549</a></p> <p>Basically the logic goes like this. How would you write your own version of <code>call_user_func()</code> ? - and then explain how that breaks with references, and how it fails when you avoid call-time pass-by-reference. In other words, the <em>right</em> way to call functions (specify the value, and let PHP decide from the function declaration whether to pass value or reference) isn't going to work when you use <code>call_user_func()</code> - you're calling two functions deep, the first by value, and the second by reference to the values in the first.</p> <p>Get your head around this, and you'll have a much deeper understanding of PHP references (and a much greater motivation to steer clear if you can).</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