On 04/18/2012 12:31 AM, Yasuo Ohgaki wrote:
> Hi,
> 
> 2012/4/18 Stefan Neufeind <neufe...@php.net>:
>> 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

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

Reply via email to