Re: Excellent language PHP5 is!
| From: | Matthew Weier O'Phinney | Date: | Thu, 19 May 2005 18:37:49 +0000 |
| Subject: | Re: Excellent language PHP5 is! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-215621@lists.php.net to get a copy of this message | ||
* 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