|
Posted by Matthew Weier O'Phinney on 10/20/69 11:16
* Robert Meyer <robert@rmpse.com>:
> 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@garden.org | http://vermontbotanical.org
Navigation:
[Reply to this message]
|