Re: Named parameters
| From: | G M | Date: | Fri, 15 Oct 2010 17:59:31 +0000 |
| Subject: | Re: Named parameters | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-49915@lists.php.net to get a copy of this message | ||
It's true that you can already do something similar -- although not as easily. I personally
always use this pattern:
function stuff($required1, $required2, $options = array()) {
extract($options);
}
This still, however, makes everyone who call my functions have to do a lot of extra typing:
stuff(1, 2, array('a' => 'b')) // well, not a lot but they have to type
array( ) EVERY TIME
instead, wouldn't it be nice if
a) Every function ever written could become extensible
b) People would be able to use that really easily: stuff(1, 2, 'a' => 'b', $c
=> $d)?
PS: Please let's NOT make it stuff(1, 2, a => 'b', c => $d) as it would take
away the option of having variable key names, which we can have in array(). Also it would create
slight ambiguity with constants.
On Oct 15, 2010, at 1:27 PM, Michael Shadle wrote:
> On Fri, Oct 15, 2010 at 10:26 AM, G M <gregory@gregory.net> wrote:
>> Okay so I am thinking about submitting a patch to PHP that would enable you to call
>> functions like this:
>>
>> stuff(1, 2, 'separator' => '<br>', 'clean' =>
>> true);
>
> I don't like having the never ending growing list of arguments issue,
> but I just use
>
> function foo($args = array()) {
>
> # first pass to normalize, check, scrub data
>
> $bar = isset($args['whatever']) ? intval($args['whatever']) : 0;
>
> # second pass to do sanity checking or business logic...
>
> if($bar > 0) {
> ...
> }
>
> }
>
> Looks like Drupal 7 is taking this approach with some things too.
>
> I am not sure the language itself needs anything to change, it can be
> done today and seems like an additional feature to shortcut something
> already available.