codebase.watch what is new in your dependencies

Options implements ArrayAccess

At a glance

Type
New feature
ArrayAccess added
2.1

Since symfony/options-resolver 2.1, Options implements ArrayAccess.

$options['key'] reads, writes, checks and removes an option, calling get(), set(), has() and remove().

Array syntax

Option set

use Symfony\Component\OptionsResolver\Options;

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

// array('key' => 'foo')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

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

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

Option read

use Symfony\Component\OptionsResolver\Options;

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

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

Option checked

use Symfony\Component\OptionsResolver\Options;

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

// Calls $options->has('key')
// true
$hasKey = isset($options['key']);

Option removed

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// Calls $options->remove('key')
unset($options['key']);

// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// Calls $options->remove('key')
unset($options['key']);

// []
$all = $options->all();

Option read by a lazy option

use Symfony\Component\OptionsResolver\Options;

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

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

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

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

Null value with isset()

use Symfony\Component\OptionsResolver\Options;

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

// Calls $options->has('key'): true, unlike with an array
// true
$hasKey = isset($options['key']);

Option without a key

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// Calls $options->set(null, 'foo'): the key of the option is ''
$options[] = 'foo';

// array('' => 'foo')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// Calls $options->set(null, 'foo'): the key of the option is ''
$options[] = 'foo';

// ['' => 'foo']
$all = $options->all();

Array value modified

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options['roles'] = array('foo');
// E_NOTICE: Indirect modification of overloaded element of Symfony\Component\OptionsResolver\Options has no effect
$options['roles'][] = 'bar';

// array('roles' => array('foo'))
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options['roles'] = ['foo'];
// E_NOTICE: Indirect modification of overloaded element of Symfony\Component\OptionsResolver\Options has no effect
$options['roles'][] = 'bar';

// ['roles' => ['foo']]
$all = $options->all();

Option missing

use Symfony\Component\OptionsResolver\Options;

$options = new Options();

// Unlike with an array, an exception is thrown
// OutOfBoundsException: The option "key" does not exist.
$options['key'];

Read-only once read

Reading an offset reads the option: the options cannot be set anymore, with the array syntax or not.

Set after a read

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

$options = new Options();
$options['key'] = 'foo';
$options['key'];

// OptionDefinitionException: Options cannot be set anymore once options have been read.
$options['key'] = 'bar';

The sources