codebase.watch what is new in your dependencies

Options::clear()

At a glance

Type
New feature
clear() added
2.1

Since symfony/options-resolver 2.1, Options::clear() removes all the options.

Remove all options

Options cleared

use Symfony\Component\OptionsResolver\Options;

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

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

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

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

Lazy options cleared

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    // Never called: the option is cleared before being read
    function (Options $options) {
        throw new LogicException('foo');
    }
);
$options->clear();

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

$options = new Options();
$options->set(
    'key',
    // Never called: the option is cleared before being read
    function (Options $options) {
        throw new LogicException('foo');
    }
);
$options->clear();

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

$options = new Options();
$options->set(
    'key',
    // Never called: the option is cleared before being read
    fn (Options $options): never => throw new LogicException('foo')
);
$options->clear();

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

Options set after

use Symfony\Component\OptionsResolver\Options;

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

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

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

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

No option

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// Does nothing: there is no option
$options->clear();

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

$options = new Options();
// Does nothing: there is no option
$options->clear();

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

The normalizers

Normalizers cleared

use Symfony\Component\OptionsResolver\Options;

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

// "email" not normalized: clear() also removes the normalizers
// array('email' => 'foo@bar.com')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

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

// "email" not normalized: clear() also removes the normalizers
// ['email' => 'foo@bar.com']
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

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

// "email" not normalized: clear() also removes the normalizers
// ['email' => 'foo@bar.com']
$all = $options->all();

Normalizer without value cleared

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();

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

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();

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

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, $value) => $value
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();

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

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, mixed $value): mixed => $value
);
// Without clear(), all() would call the normalizer and create the option
$options->clear();

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

Read-only once read

Once an option is read, the options cannot be cleared anymore.

Cleared after a read

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 cleared anymore once options have been read.
$options->clear();

The sources