setDefault()
setDefault(string $option, mixed $value) : $this
Sets the default value of a given option.
If the default value should be set based on other options, you can pass
a closure with the following signature:
function (Options $options) {
// ...
}
The closure will be evaluated when {@link resolve()} is called. The
closure has access to the resolved values of other options through the
passed {@link Options} instance:
function (Options $options) {
if (isset($options['port'])) {
// ...
}
}
If you want to access the previously set default value, add a second
argument to the closure's signature:
$options->setDefault('name', 'Default Name');
$options->setDefault('name', function (Options $options, $previousValue) {
// 'Default Name' === $previousValue
});
This is mostly useful if the configuration of the {@link Options} object
is spread across different locations of your code, such as base and
sub-classes.
If you want to define nested options, you can pass a closure with the
following signature:
$options->setDefault('database', function (OptionsResolver $resolver) {
$resolver->setDefined(['dbname', 'host', 'port', 'user', 'pass']);
}
To get access to the parent options, add a second argument to the closure's
signature:
function (OptionsResolver $resolver, Options $parent) {
// 'default' === $parent['connection']
}
Parameters
string |
$option |
The name of the option
|
mixed |
$value |
The default value of the option
|
Throws
- \Symfony\Component\OptionsResolver\Exception\AccessException
If called from a lazy option or normalizer
Returns
$this
—