Re: How can I use a function's default arguments but change one of the end ones in the list?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, Feb 5, 2009 at 20:10, German Geek <geek.de@xxxxxxxxx> wrote:
> I've thought about this problem before but couldn't think of a solution
> either. How does func_get_args() solve this? You could make a wrapper
> function without that.

    It's all in how it's used.  Ironically, I don't remember saying
that you couldn't make a wrapper function without it, or that it was
required to make a wrapper.  Thanks for clarifying that for everyone
just in case.  ;-P

> How would u (php) know which parameter u mean in a particular case?

    You don't have to.  You can send NULL or empty strings for that
matter, and use the parameter position in func_get_args() that way.
Or using hierarchy and preference:

<?php
    if(func_num_args() == '2') {
        $first_param = func_get_args(1);
        $second_param = 'default_value';
        $third_param = func_get_args(2);
        $fourth_param = 'another default here';
    }
?>

    Though inelegant, it works.

    What would probably be better, though, would be to write the
wrapper as I mentioned, but have it accept a single parameter as an
array, like so:

<?php
function wrapper_func($arr) {
    // Define variable defaults
    $param_one = null;
    $param_two = 'apple';
    $param_three = 'foo';
    $param_four = 123.45;

    // Redefine variables with values passed to __FUNCTION__
    foreach($arr as $k => $v) {
        $$k = $v;
    }

    // Call original function with these values and pass through the results.
    return orig_func($param_one,$param_two,$param_three,$param_four);
}

// Then use it like so:
$params = array(
        'param_two' => 'Orange',
        'param_four' => 543.21,
    );
?>

    If all orig_func() did was a simple foreach()/echo routine, it
wrapper_func() would print:

[NULL]
Orange
foo
543.21

> I think it would just be useful to have an IDE that can write out the
> default parameters on a keyboard shortcut or mouse click and then u can
> change them afterwards.

    It would be nice.... if every programmer touching the code had the
same IDE.  Unfortunately, it's just not practical.

-- 
</Daniel P. Brown>
daniel.brown@xxxxxxxxxxxx || danbrown@xxxxxxx
http://www.parasane.net/ || http://www.pilotpig.net/
Unadvertised dedicated server deals, too low to print - email me to find out!

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux