Okay, so it seems to me based on the discussion, that if we implement named parameters the following way, it should work well:

1) Allow named parameters in function calls, using the syntax func($a, $b, 'var1' => $value, $var2 => $value2), similar to array() definition.

2) Do not allow non-named parameters to follow named ones, for example func($a, 'var1' => $value, $b) is not allowed.

3) Internally, this would just create an additional hash (if and only if named parameters were specified in the call) and pass it as the last parameter, implicitly. It would have the same overhead as creating and passing an actual PHP array.

4) Internally, extend the func_get_args() function to check for the presence of this last parameter. If it exists, then merge it on top of the array before returning from func_get_args().

This way we can use the existing stack mechanism and still everything will work, with similar overhead to passing an $options array like programmers do now. The only addition will be a "special" implicit hash tacked on as the last parameter.

(There are two ways to handle this. One way is to always push the hash onto the stack, and later check if its length = 0. The second way is to push the hash onto the stack only if named parameters were specified. In this case, we'll need to set a flag during the call to indicate whether the hash was pushed or not.)

That's all. I think this is very doable. What are your thoughts?

Greg

PS: Also, what do you guys think of an additional (optional) possibility:

function myFunc($a, $b, ...$options) // sort of like Java

and this way $options can be filled, so people don't have to keep using func_get_args to get all the arguments.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to