codebase.watch what is new in your dependencies

Options::get($option)

At a glance

Type
New feature
get() added
2.1

Since symfony/options-resolver 2.1, Options::get() reads the value of an option.

Read an option

Option set

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');

// 'foo'
$key = $options->get('key');

Null value

An option set to null exists: its value is returned.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', null);

// null
$key = $options->get('key');

Array syntax

Options implements ArrayAccess: reading an offset calls get().

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');

// Calls $options->get('key')
// 'foo'
$key = $options['key'];

Option missing

use Symfony\Component\OptionsResolver\Options;

$options = new Options();

// OutOfBoundsException: The option "key" does not exist.
$options->get('key');

Lazy options

Lazy value evaluated

The closure of a lazy option is called when the option is read, and its result is returned.

The order of the calls to set() does not matter: firstName can be set after key.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set(
    'key',
    function (Options $options) {
        return strtoupper($options->get('firstName'));
    }
);

// 'FOO'
$key = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set(
    'key',
    fn (Options $options): string => strtoupper($options->get('firstName'))
);

// 'FOO'
$key = $options->get('key');

Evaluated once

The closure is only called by the first read: its result replaces it, so uniqid() is called once.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    function (Options $options) {
        return uniqid();
    }
);

// '6720f3b1c8a4e'
$firstKey = $options->get('key');
// '6720f3b1c8a4e'
$secondKey = $options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    fn (Options $options): string => uniqid()
);

// '6720f3b1c8a4e'
$firstKey = $options->get('key');
// '6720f3b1c8a4e'
$secondKey = $options->get('key');

Cyclic dependency

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'firstName',
    function (Options $options) {
        return $options->get('lastName');
    }
);
$options->set(
    'lastName',
    function (Options $options) {
        return $options->get('firstName');
    }
);

// OptionDefinitionException: The options "firstName", "lastName" have a cyclic dependency.
$options->get('firstName');
use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'firstName',
    fn (Options $options): string => $options->get('lastName')
);
$options->set(
    'lastName',
    fn (Options $options): string => $options->get('firstName')
);

// OptionDefinitionException: The options "firstName", "lastName" have a cyclic dependency.
$options->get('firstName');

Normalized options

Option normalized

The normalizer is only called by the first read: its result replaces the value, so uniqid() is called once.

The order of the calls to set() and setNormalizer() does not matter: the normalizer can be set before the value.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return uniqid() . '_' . strtoupper($value);
    }
);

// '6720f3b1c8a4e_FOO@BAR.COM'
$firstEmail = $options->get('email');
// '6720f3b1c8a4e_FOO@BAR.COM'
$secondEmail = $options->get('email');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    fn (Options $options, string $value): string => uniqid() . '_' . strtoupper($value)
);

// '6720f3b1c8a4e_FOO@BAR.COM'
$firstEmail = $options->get('email');
// '6720f3b1c8a4e_FOO@BAR.COM'
$secondEmail = $options->get('email');

Lazy value normalized

The lazy value is evaluated first, then given to the normalizer.

The order of the calls to set() and setNormalizer() does not matter: the lazy value is always evaluated before the normalizer.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'email',
    // Evaluated first, by the first read of the option
    function (Options $options) {
        return 'foo@bar.com';
    }
);
$options->setNormalizer(
    'email',
    // Evaluated second, receives in $value the result of the lazy option
    function (Options $options, $value) {
        return strtoupper($value);
    }
);

// 'FOO@BAR.COM'
$email = $options->get('email');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'email',
    // Evaluated first, by the first read of the option
    fn (Options $options): string => 'foo@bar.com'
);
$options->setNormalizer(
    'email',
    // Evaluated second, receives in $value the result of the lazy option
    fn (Options $options, string $value): string => strtoupper($value)
);

// 'FOO@BAR.COM'
$email = $options->get('email');

Normalizer without value

A normalizer does not create the option.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return strtoupper($value);
    }
);

// OutOfBoundsException: The option "email" does not exist.
$options->get('email');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, string $value): string => strtoupper($value)
);

// OutOfBoundsException: The option "email" does not exist.
$options->get('email');

Read-only once read

Once an option is read, the options cannot be set, overloaded, replaced, removed or cleared anymore.

Set after a missing option

get() locks the options before checking that the option exists: they stay locked after the exception.

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();

try {
    $options->get('firstName');
} catch (OutOfBoundsException $exception) {}

// OptionDefinitionException: Options cannot be set anymore once options have been read.
$options->set('firstName', 'foo');

Set after empty()

empty() on an offset calls offsetExists(), then offsetGet() when the option exists, which calls get().

Unlike isset(), it reads the option: the options are locked.

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// Calls $options->has('key'), then $options->get('key')
empty($options['key']);

// OptionDefinitionException: Options cannot be set anymore once options have been read.
$options->set('key', 'bar');

Set after a read

The order of the calls matters: set() works before the first get(), not after.

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->get('key');

// OptionDefinitionException: Options cannot be set anymore once options have been read.
$options->set('key', 'bar');

The sources