NYCPHP Meetup

NYPHP.org

[nycphp-talk] passing optional arguments by reference

Scott Mattocks crisscott at netzero.com
Fri Jan 16 12:58:19 EST 2004


Daniel Convissor wrote:

 > I'm want the parameters for a function to be passed by reference.
 > Normally, that's accomplished by placing a & in front of the variable.
 > Trick is, the argument in question needs to be optional.  Placing a & in
 > front of an optional parameter creates a parse error.

Can it be done with some combination of func_num_args and func_get_args?

function &execute($stmt) {

   if (func_num_args () > 1) {
     $args = func_get_args ();
     if (!is_array ($args[1]))
       $args[1] = array ()
   }
   // The rest of the code...
}

This should avoid the wrong number of parameter errors I think and still 
allow give you the flexibility of passing the second parameter by reference.

Scott Mattocks






More information about the talk mailing list