Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:27257 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 21102 invoked by uid 1010); 4 Jan 2007 11:12:20 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 21087 invoked from network); 4 Jan 2007 11:12:20 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Jan 2007 11:12:20 -0000 Authentication-Results: pb1.pair.com header.from=devriese@cs.tcd.ie; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=edsko@netsoc.tcd.ie; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain netsoc.tcd.ie from 134.226.83.42 cause and error) X-PHP-List-Original-Sender: edsko@netsoc.tcd.ie X-Host-Fingerprint: 134.226.83.42 spoon.netsoc.tcd.ie Linux 2.6 Received: from [134.226.83.42] ([134.226.83.42:47807] helo=spoon.netsoc.tcd.ie) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id EF/5A-20008-5B0EC954 for ; Thu, 04 Jan 2007 06:10:47 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by spoon.netsoc.tcd.ie (Postfix) with ESMTP id D4BB754085 for ; Thu, 4 Jan 2007 11:10:41 +0000 (GMT) Received: from spoon.netsoc.tcd.ie ([127.0.0.1]) by localhost (spoon.netsoc.tcd.ie [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 01570-05 for ; Thu, 4 Jan 2007 11:10:41 +0000 (GMT) Received: by spoon.netsoc.tcd.ie (Postfix, from userid 1025) id AA05254585; Thu, 4 Jan 2007 11:10:41 +0000 (GMT) Date: Thu, 4 Jan 2007 11:10:41 +0000 To: internals@lists.php.net Message-ID: <20070104111041.GA21597@netsoc.tcd.ie> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Operating-System: Linux spoon.netsoc.tcd.ie 2.6.17 User-Agent: Mutt/1.5.9i X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at spoon.netsoc.tcd.ie Subject: Separation anxiety From: devriese@cs.tcd.ie (Edsko de Vries) 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