Named parameters

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

recently I noticed that it is syntactically correct to write
$ret = my_func($a = 1, $b = 2, $c = 'blah');

It seems that semantically this is totally useless.

Or is there any use for this? Maybe one can get those names at runtime
(backtrace / Reflection API?) to emulate named parameters?


Re: Named parameters

Quoted text here. Click to load it

Not true, but this almost certainly does not do what you think it does. I'm
guessing you had this:

   function my_func( $a, $b, $c )

and are thinking that the names on the function call are related to the
parameter names.  Not true.  What you wrote is semantically identical to

   $a = 1;
   $b = 2;
   $c = 'blah';
   $ret = my_func( $a, $b, $c );

You created three new local variables.  You can check this by printing $c
immediately after the function call.  You'll find it now contains 'blah'.
You could have written this:

   $ret = my_func( $xx = $yy = 1, $qqq = 2, $www = 'blah' );

and it would also work.

Quoted text here. Click to load it

It's just a side effect of the C-like philosophy that assignment is just
another operator.
Tim Roberts,
Providenza & Boekelheide, Inc.

Re: Named parameters

Quoted text here. Click to load it

Oh, of course. Thx.

Site Timeline