Forum Moderators: coopster

Message Too Old, No Replies

optional function arguments

How to let them keep there values

         

BlackDex

9:27 am on Dec 29, 2005 (gmt 0)

10+ Year Member



Hello ppl :).

Is there a way to have an optional argument keep there default value?

Example:

function test($required1, $required2, $optional1 = 'test1', $optional2 = 'test2') { 
//do something
}

test('Req1', 'Req2', , 'Test2New');

So that $optional1 will stay 'test1' and i just skip it.

The way it is show above doesn't work and gives an error about an argument is missing.

Is there a way to let it have its value without having to type it again?

Thx in advanced.

adb64

10:50 am on Dec 29, 2005 (gmt 0)

10+ Year Member



Hi BlackDex

For as far as I know this can not be done, but a workaround I use in these (rare) cases is the following:


function test($required1, $required2, $optional1 = FALSE, $optional2 = 'test2')
{
if ($optional1 === FALSE)
{
$optional1 = 'test1';
}
//do something
}

Assign $optional1 a default value of a type which it will never receive when being called, type Boolean in this example.
The function should be called then:


test('Req1', 'Req2', FALSE , 'Test2New');

Of course now you don't really 'skip' the argument but when calling the function you don't have to bother about the default value to fill in.
The same can be done for $optional2, but as it is the last argument there is no need to do it.

Regards,
Arjan

BlackDex

11:05 am on Dec 29, 2005 (gmt 0)

10+ Year Member



Thx for the help..
This clears things up..

I Already thought it whas not possible, but hoped i overlooked something.

Owell.. thx for the workaround :).

jatar_k

4:56 pm on Dec 29, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when leaving out arguments you must leave out everything after the first you wish to leave out, you can't skip one

if you most often leave out the one you presently have next to last then maybe you should make it the last argument, otherwise I would say you logic is flawed.

<added>that workaround makes no sense

you are still passing false and having it default to another value, the default false in the function definition is redundant. Removing the default value of false would produce the same result as if you have it there.

again, logic flaw

Mr_Fern

8:49 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



As jatar mentioned, default values are only available if they are the last variables.

Example Function Declaration
demo(a, b = 2, c = 3, d = 4)

b,c,d will retain 2,3,4 respectively if you demo(a_value) but b will not have a value if you demo(a_value,,c_value,d_value)

The best way to work around this, is to rewrite the function in the manner of whatever will be left out would be the last value.

Or outside of the call, set up a comparison to just use the default values.

i.e.

$req1, $req2, $opt1, $opt2 are the variables being sent to test()

$opt1 = (empty($opt1))? 'default_value' : $opt1;

with that code, you can ensure that opt1 either has a value, or the default value.

BlackDex

10:48 pm on Dec 29, 2005 (gmt 0)

10+ Year Member



Wel my prob is,
that every value could be optional.
Depends if you need it set manualy or not.

Don't ask why, it just is :D.

But this last option looks nice indeed..
No true/false :).

thx

adb64

1:25 pm on Dec 30, 2005 (gmt 0)

10+ Year Member



you are still passing false and having it default to another value, the default false in the function definition is redundant. Removing the default value of false would produce the same result as if you have it there.

I wouldn't say the that the default false is redundant. In case you want argument 3 to have its default value and assign a non-default value to argument 4, you must have some way to distinguish between a default value and an assigned value for argument 3.
If the function is called in this way from 812 different places in your code and the default value for argument 3 must be changed, you have to change argument 3 in all those calls. When false is being used, the new default only has to be changed in one place, in the function where it belongs.

Also the option given by Mr_Fern won't work in that particular situation (although it looks a lot like my solution), you still have to fill in a value for argument 3, as you may not skip it, when the function is called with argument 4 set to a non-default value. In that situation empty($opt1) is false then and the passed value will be set in $opt1 and not the default value. So in that case you still have to change 812 function calls when the default value for argument 3 is changed.

By the way, I agree that when argument 3 has its default most of the calls and argument 4 not, change their order. Or make different functions for all situations and let those functions fill in the default values before they call test(...).

Regards,
Arjan