Hi,

In her excellent book, Sara Golemon describes how PHP's refcount/is_ref
mechanism works, and the situations in which this leads to problems
(section "separation anxiety"). One of the examples is

$a = 1;
$b =& $a;
$c = $a;

Since $a is a "real" reference, $c cannot be made a copy-on-write
reference to $a, and is therefore "separated": it will point to a copy
of $a. If the last line had been

$c =& $a;

No problem would have arisen, and $c would simply have been added to the
change-on-write set associated with $a (i.e., it would point to $a and
$a's refcount would have been upped). So far, as good - that's all
clear.

Now, it is my understanding that a similar thing happens when a function
is called:

function f($arg) { ... }

$a = 1;
$b =& $a;
f($a);

The last line is analogous in some sense to $arg = $a, and so we have
the same situation as before: $arg will be associated with a _copy_ of
$a. From an extension-writer point, if f is written in C in some
extension, I don't have to worry about this, the PHP engine will do it
for me. If the function had been declared as

function f(&$arg) { ... }

the last line would be anaologus to $arg =& $a", and no separation would
have been necessary. Again, the engine does this for me, I don't have to
worry about it. 

If the line "$b =& $a" would be "$b = $a", $a would be in a
copy-on-write set, and the extension function would itself be reponsible
for separating it, if necessary (although zend_parse_parameters can do
this if the "/" option is used). The engine doesn't automatically do
this for me. Correct me if I'm wrong so far.

Now what I'm wondering about is this: if I call a user function with
call_user_function, the perspective changes slightly: now I am the one
passing in the arguments. My question is this: if I call a user function
with call_user_function, am I responsible for separating the arguments
if they are in a change-on-write set? In other words, if I am passing a
zval to a function (that I call using call_user_function), and that zval
is a real reference, but the function does not expect a reference,
should I separate it first? If so, does that mean I need to know the
function's signature (or at least, whether it expects a reference or
not) before I can call it using call_user_function?

Thanks,

Edsko

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to