Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:60157 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99571 invoked from network); 18 Apr 2012 06:46:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Apr 2012 06:46:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=neufeind@php.net; spf=unknown; sender-id=unknown Authentication-Results: pb1.pair.com header.from=neufeind@php.net; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 91.184.32.3 as permitted sender) X-PHP-List-Original-Sender: neufeind@php.net X-Host-Fingerprint: 91.184.32.3 mail.speedpartner.de Linux 2.5 (sometimes 2.4) (4) Received: from [91.184.32.3] ([91.184.32.3:58781] helo=mail.speedpartner.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4F/36-21594-D536E8F4 for ; Wed, 18 Apr 2012 02:46:54 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by mail.speedpartner.de (Postfix) with ESMTP id AE110B3DDE for ; Wed, 18 Apr 2012 08:46:50 +0200 (CEST) Received: from mail.speedpartner.de ([127.0.0.1]) by localhost (mail.speedpartner.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id J07-hGdFK7GD for ; Wed, 18 Apr 2012 08:46:50 +0200 (CEST) Received: from collab.speedpartner.de (collab.speedpartner.de [91.184.32.10]) by mail.speedpartner.de (Postfix) with ESMTP id 9335EB3DDD for ; Wed, 18 Apr 2012 08:46:50 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by collab.speedpartner.de (Postfix) with ESMTP id 50870CACFB9 for ; Wed, 18 Apr 2012 08:46:48 +0200 (CEST) X-Virus-Scanned: amavisd-new at collab.speedpartner.de Received: from collab.speedpartner.de ([127.0.0.1]) by localhost (collab.speedpartner.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id L2WKPoqsEg1s for ; Wed, 18 Apr 2012 08:46:47 +0200 (CEST) Received: from [192.168.4.28] (ip-62-143-28-82.unitymediagroup.de [62.143.28.82]) by collab.speedpartner.de (Postfix) with ESMTPSA id A8325CACF48 for ; Wed, 18 Apr 2012 08:46:47 +0200 (CEST) Message-ID: <4F8E6348.2050702@php.net> Date: Wed, 18 Apr 2012 08:46:32 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1 MIME-Version: 1.0 To: php-dev References: <4F8DE877.1010600@php.net> <4F8DEA47.9060706@php.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Variable-length args by reference From: neufeind@php.net (Stefan Neufeind) On 04/18/2012 12:31 AM, Yasuo Ohgaki wrote: > Hi, > > 2012/4/18 Stefan Neufeind : >> On 04/18/2012 12:02 AM, Stefan Neufeind wrote: >>> Hi, >>> >>> the topic of variable argument-lists for functions in connection with >>> getting the parameters by reference came up. This is currently not >>> possible with func_get_args(), though a "hack" with debug_backtrace() is >>> possible. [...] > I don't know what you would like to do, but > > function calc(&$args = NULL) { > var_dump(count($args)); // num of args > $args[1] *= 2; > } > calc(array(1,2,3,4)); > > would work. Hi, well yes, but that's not a variable number of parameters then :-) As Johannes pointed out the problem is that PHP needs to know before calling the function that it needs to work with references. Calling calc($a, $b) (with no call-time pass-by-referefence of course) I've tried the following things out: function calc(&$dummy0 = NULL, &$dummy1 = NULL) { $d = &debug_backtrace(); $args = $d[0]['args']; foreach($args as &$a) { $a *= 2; } } => Can have up to two "variable" parameters (since they have a default of NULL). The workaround for allowing "more" variable would be to add more dummies then *sigh* but would work. What does not work: function calc(&$dummy0 = NULL, &$dummy1 = NULL) { $args = func_get_args(); foreach($args as &$a) { $a *= 2; } } since func_get_args() returns copies one way or the other. Would it maybe be a good idea for func_get_args() to return an array with references (as debug_backtrace() does) in case the parameters were references when the function was called? Kind regards, Stefan