Re: Excellent language PHP5 is!

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Robert Meyer <robert@xxxxxxxxx>:
> There is one thing I hope is implemented in PHP soon.  That is the ability 
> to pass by reference and receive the reference in variable parameter lists. 
> You can pass by reference to a function taking a variable number of 
> parameters, but func_get_arg() only returns the value and therefore, any 
> changes to that variable do not change the variable as seen by the caller. 
> Maybe a func_get_refarg() could be implemented.

Indeed, there's a comment on the func_get_arg() manual page to this very
effect.

One possibility is to wrap the reference into an array:

    // in functioncall():
    function functioncall() {
        $args = func_get_arg(0);
        $arg =& $args[0]; // retrieving reference from array
        $arg++;
    }

    $arg = 1;
    functioncall(array(&$arg)); // passing an array with a ref
    echo $arg;

I tested this, and it works. Requires a bit more work in the function,
but a little architecting could get it working.

Another possiblity is to pass associative arrays to your functions;
then, in your function, check for the existence of keys you need. The
beauty of this approach is that you don't have to muck about with the
func_get_arg() calls, you still get a variable number of arguments, and
you can easily pass references.

-- 
Matthew Weier O'Phinney           | WEBSITES:
Webmaster and IT Specialist       | http://www.garden.org
National Gardening Association    | http://www.kidsgardening.com
802-863-5251 x156                 | http://nationalgardenmonth.org
mailto:matthew@xxxxxxxxxx         | http://vermontbotanical.org

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux