Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Named parameters
- AndrÃ© HÃ¤nsel
April 11, 2009, 4:49 am
rate this thread
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
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.
It's just a side effect of the C-like philosophy that assignment is just
Tim Roberts, firstname.lastname@example.org
Providenza & Boekelheide, Inc.
- » inserts into 4 mln records table can cause problems?
- — Next thread in » PHP Scripting Forum