/    Sign up×
Community /Pin to ProfileBookmark

Constructor with optional parameter list

Hello,

I have a general question about PHP and functions with variable parameter lists.
The question is mostly, what solution you would consider as best “style”.

The standard solution would be, to define a standard value for each parameter. Two problems arise:
1.) I do want to force the user to specify all parameters, standard values would not make sense (of course I could throw an error)
2.) I do not know which parameters were passed! Now you will say just check if a var has standard value and if so, assume it was not passed – but what about “bool” parameters (true and false, both could be passed value)
This second problem is what really makes me frustrated. How is it possible that there is no way to figure out which var was passed and which wasn’t. Even func_get_args won’t tell you…

Another solution I see pretty often is, to use one array as parameter whenever you write a constructor function. Here I see two problems again:
1.) same as above
2.) It requires the user to construct that array to pass each time he wants to create a new object. Of course (like often done) you can define a default array for object creation but this still would (most likely) have to be altered each time.
This solution therefore seems pretty ugly to me.

How do experienced PHP developers solve this dilemma?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiApr 21.2009 — Not sure I follow why func_get_args won't work, but personally Ill tend to allow an associative array as a parameter and then define a default array inside the constructor and array_merge them:

[code=php]function myFunc(array $params) {
$defaults = array(
'param1' => 'foo',
'param2' => 'bar',
'param3' => 'baz'
)
$params = array_merge($defaults, $params);
// ... etc
}[/code]
Copy linkTweet thisAlerts:
@NogDogApr 21.2009 — I'm not sure I understand what you are referring to in "1)"? If you provide a parameter list with no default parameter values, then all parameters are required and not providing all of them will generate a fatal error; thus the client code is "forced" to provide all parameters. Am I missing or misunderstanding something here?
Copy linkTweet thisAlerts:
@deck42authorApr 21.2009 — Thanks for your answers!

@Mindzai:

You are right, I was not right about "func_get_args", since php doesn't allow to "leave out" parameters the behaviour of "func_get_args" to return only "passed" variables will tell you which were passed and which are standard values.

Also thanks for your example of how you use arrays.

@Nogdog:

What you say is absolutely correct of course, but that is just what I was referring to:

The standard solution would be, [b]to define a standard value for each parameter.[/b] Two problems arise:

1.)
[/QUOTE]

So "1.)" was referring to the case if you DO specify standard values for all parameters.

Thanks again, I guess there will be no better solution than to use arrays..
Copy linkTweet thisAlerts:
@NogDogApr 21.2009 — I guess we're still not communicating. I see you saying "I do want to force the user to specify all parameters", which would be simply accomplished by providing a parameter list with no default values. The parser will require that any client code supplies those parameters.

If your intent is actually [i]not[/i] to force the specification of some/all parameters but you need to know for some reason that the client code did not supply one or more of them, you could set the default value to null, then check each such parameter to see if it's null, and if so, do whatever it is you want to do when a parameter is omitted.
[code=php]
<?php

function foo($var1 = null, $var2 = null, $var3 = null)
{
$defaults = array(
'var1' => 'default 1',
'var2' => 'default 2',
'var3' => 'default 3'
);
foreach($defaults as $key => $value)
{
if(${$key} === null)
{
${$key} = $defaults[$key];
user_error("Default value being used for '$key'");
}
}
echo "$var1 $var2 $var3<br />n";
}

foo();
foo('bar');
foo('one', 'two');
[/code]
×

Success!

Help @deck42 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...