OptionsResolver::setDefault()

OptionsResolver setDefault(string $option, mixed $value)

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.

Parameters

string $option The name of the option
mixed $value The default value of the option

Return Value

OptionsResolver This instance

Exceptions

AccessException If called from a lazy option or normalizer
doc_Symfony
2016-10-28 06:25:17
Comments
Leave a Comment

Please login to continue.