Skip to content

Core\Config::restore

Puts a setting back to the value the configuration file gives it, or to its built-in default.

Core\Config::set changes a setting for this request only. restore deletes that change, so the setting is again what the configuration file says. A program that changed a setting for one step can undo it afterwards. The name is read the way Core\Config::get reads it, so a limit can be written with its short name, memory, or with its full name, limits.memory. One call puts back one setting.

Good to know: a name your program never changed is allowed. There is nothing to undo, so nothing happens and there is no error. When the configuration file does not write the setting, it gets its built-in default again. A setting with no default, such as wall_time, is null again.

Code in a package can only undo a change that a package made. If your program set a value before the package changed it, restore in the package puts your program's value back.

Parameters

  • $namestringThe dotted name of the setting, such as limits.memory, as get takes it. The compiler checks a name written as text in the call. If set may never change that setting, the program does not compile.

Return value

Returns void. Nothing. A name this request never changed is not an error. There is nothing to undo.

Changelog

VersionChange
0.0.6Core\Config::set changes only the settings on one list, and the page of Core\Config::set shows the list. A name you write in the call that is not on the list gives the error E0667. A template string that no name on the list fits gives the warning W1037.

Examples

Undo one change

<?nvs
// Shows how to undo a change to a setting. `Core\Config::restore` puts back the
// value the configuration file gives, or the built-in default.
Core\Config::set('max_regex_steps', '5000');
// This prints 5000 and then 1000000. The configuration file here does not set
// this limit, so `restore` puts back its built-in default.
echo Core\Config::get('max_regex_steps') ?? 'no limit', "\n";
Core\Config::restore('max_regex_steps');
echo Core\Config::get('max_regex_steps') ?? 'no limit', "\n";
Output
5000
1000000

Put back every change

<?nvs
// Shows how a program puts back every setting it changed. One call puts back
// one setting, so a program that changed several of them keeps their names.
array<string> $mine = ['max_tasks', 'wall_time'];
Core\Config::set('max_tasks', '4');
Core\Config::set('wall_time', '90s');
// This prints 4 and 90s.
echo Core\Config::get('max_tasks') ?? 'not set', ' and ', Core\Config::get('wall_time') ?? 'not set', "\n";
foreach ($mine as string $name) {
Core\Config::restore($name);
}
// This prints not set and not set. The configuration file here sets neither
// limit, and neither limit has a built-in default.
echo Core\Config::get('max_tasks') ?? 'not set', ' and ', Core\Config::get('wall_time') ?? 'not set', "\n";
Output
4 and 90s
not set and not set

More time for one job

<?nvs
// Shows how a program gives itself more time for one slow job and then puts the
// setting back. The work after it runs under the configuration file's own value
// again.
// The monthly report takes longer than a normal request.
Core\Config::set('wall_time', '300s');
echo 'the report runs with ', Core\Config::get('wall_time') ?? 'no limit of its own', "\n";
// The report would run here.
Core\Config::restore('wall_time');
// This prints: the work after it runs with no limit of its own. The
// configuration file here sets no time limit, and `wall_time` has no default.
echo 'the work after it runs with ', Core\Config::get('wall_time') ?? 'no limit of its own', "\n";
Output
the report runs with 300s
the work after it runs with no limit of its own