Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] Variable-length args by reference

Posted by Stefan Neufeind 
Stefan Neufeind
[PHP-DEV] Variable-length args by reference
April 18, 2012 12:10AM
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.

How would you think about extending func_get_args() and func_get_arg()
to allow for:

$args = func_get_args(FUNC_GET_ARGS_BY_REFERENCE);
$arg0 = func_get_arg (0, FUNC_GET_ARGS_BY_REFERENCE);

(default would be FUNC_GET_ARGS_BY_COPY)


Currently only the following "hack" works:

function calc()
{
$d = debug_backtrace();
$args = $d[0]['args'];
$args[0] *= 2;
$args[1] *= 2;
}

$a = 5;
$b = 7;
var_dump ($a, $b);
calc(&$a, &$b);
var_dump ($a, $b);


Kind regards,
Stefan

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Stefan Neufeind
Re: [PHP-DEV] Variable-length args by reference
April 18, 2012 12:20AM
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.
>
> How would you think about extending func_get_args() and func_get_arg()
> to allow for:
>
> $args = func_get_args(FUNC_GET_ARGS_BY_REFERENCE);
> $arg0 = func_get_arg (0, FUNC_GET_ARGS_BY_REFERENCE);
>
> (default would be FUNC_GET_ARGS_BY_COPY)
>
>
> Currently only the following "hack" works:
>
> function calc()
> {
> $d = debug_backtrace();
> $args = $d[0]['args'];
> $args[0] *= 2;
> $args[1] *= 2;
> }
>
> $a = 5;
> $b = 7;
> var_dump ($a, $b);
> calc(&$a, &$b);
> var_dump ($a, $b);

Hmm, that example also only works because of the call-time
pass-by-reference? *sigh*
Sorry, I forgot those ampersands in the call to calc(). Doesn't work
without.

I hope the idea is clear though. Any chance we might make this possible
through func_get_args()?


Regards,
Stefan

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Yasuo Ohgaki
Re: [PHP-DEV] Variable-length args by reference
April 18, 2012 12:40AM
Hi,

2012/4/18 Stefan Neufeind <[email protected]>:
> 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.
>>
>> How would you think about extending func_get_args() and func_get_arg()
>> to allow for:
>>
>>       $args = func_get_args(FUNC_GET_ARGS_BY_REFERENCE);
>>       $arg0 = func_get_arg (0, FUNC_GET_ARGS_BY_REFERENCE);
>>
>> (default would be FUNC_GET_ARGS_BY_COPY)
>>
>>
>> Currently only the following "hack" works:
>>
>> function calc()
>> {
>>       $d = debug_backtrace();
>>         $args = $d[0]['args'];
>>         $args[0] *= 2;
>>         $args[1] *= 2;
>> }
>>
>> $a = 5;
>> $b = 7;
>> var_dump ($a, $b);
>> calc(&$a, &$b);
>> var_dump ($a, $b);
>
> Hmm, that example also only works because of the call-time
> pass-by-reference? *sigh*
> Sorry, I forgot those ampersands in the call to calc(). Doesn't work
> without.
>
> I hope the idea is clear though. Any chance we might make this possible
> through func_get_args()?

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.

Regards,

--
Yasuo Ohgaki

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Johannes Schlüter
Re: [PHP-DEV] Variable-length args by reference
April 18, 2012 12:50AM
On Wed, 2012-04-18 at 00:02 +0200, Stefan Neufeind wrote:
> How would you think about extending func_get_args() and func_get_arg()
> to allow for:
>
> $args = func_get_args(FUNC_GET_ARGS_BY_REFERENCE);
> $arg0 = func_get_arg (0, FUNC_GET_ARGS_BY_REFERENCE);
>
> (default would be FUNC_GET_ARGS_BY_COPY)

Well we have to know the type before actually calling the function as
for references we need something we can write to. So unless somebody
comes up with a clever trick this would require a new language
construct.

And well, as a sidenote which I mention often: Don't use references :-)

johannes



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Stefan Neufeind
Re: [PHP-DEV] Variable-length args by reference
April 18, 2012 08:50AM
On 04/18/2012 12:31 AM, Yasuo Ohgaki wrote:
> Hi,
>
> 2012/4/18 Stefan Neufeind <[email protected]>:
>> 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
Sorry, only registered users may post in this forum.

Click here to login