Skip to content

Core\Config::set

Changes one setting for this request.

No other request sees the change, and it ends with the request. The value is written as in a configuration file: 512M, 30s, 64.

set returns true when the change was made. It returns false when the change is not allowed, and the old value stays.

A request may change only these settings:

  • [mode]: default
  • [limits]: memory, cpu_time, wall_time, max_output, request_body, upload_total, max_multipart_parts, max_tasks, max_regex_steps
  • [log]: format, level
  • [errors]: deprecated
  • [deferred]: deadline
  • [debug]: inline, store
  • [http.headers]: content_type_options, frame_ancestors, referrer_policy, hsts, hsts_subdomains, content_security_policy, permissions_policy
  • [http.cors]: origins, expose, credentials
  • [http.cookies]: secure, http_only, same_site, path
  • [http.client]: connect_timeout, deadline, max_redirects, idle, max_duration
  • [http.client.socket]: max_message, send_timeout

A name in the call that is not in this list does not compile.

A limit has a ceiling: the highest value a request may set. It is the limit's value in [limits.hard], or else its value in [limits]. mode.default takes development only when [mode] ceiling allows it. debug.inline and debug.store take only false.

Code in a package (a library your program installs) may only lower a [limits] setting. Core\Config::restore puts back the value from the configuration file.

Parameters

  • $namestringThe dotted name of the setting, such as limits.memory, as get takes it. A request may change only some settings of [mode], [limits], [log], [errors], [deferred], [debug] and [http]. Your editor offers each one, and the page of Core\Config::set lists them. If you write a name in the call that a request may never change, the program does not compile.
  • $valuestringThe value, written the way the configuration file writes it, such as 512M, 30s or 64.

Return value

Returns bool. true when the change was made. false when the change is not allowed, and the old value stays. A change is not allowed for a setting a request may not change. It is not allowed for a value that is not a valid size, duration or count. A limit has a ceiling, the highest value a request may set. A value above it is not allowed. The ceiling is the limit's value in [limits.hard]. Without one, the ceiling is the limit's value in nvs.toml. debug.inline and debug.store take only false. set never throws an error.

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

Change one setting

<?nvs
// Shows how to change one setting for this request. `Core\Config::set` returns
// `true` when the change was made.
// A limit can be written with its short name, `memory`. The value is written
// the way a configuration file writes it.
if (Core\Config::set('memory', '384M')) {
// This prints: this request may use 384M.
echo 'this request may use ', Core\Config::get('memory') ?? 'no limit', "\n";
}
Output
this request may use 384M

A change that is not allowed

<?nvs
// Shows what a change that is not allowed looks like. `Core\Config::set`
// returns `false`, and the setting keeps its old value. It throws no error,
// so check the result.
// Only the person who runs the server may set this one, so `Core\Config::set('opcache.file_cache_dir', ...)`
// does not compile. Here the name is in a variable, so the call runs and returns `false`.
string $operatorOnly = 'opcache.file_cache_dir';
echo Core\Config::set($operatorOnly, '/tmp/nvs') ? 'changed' : 'not allowed', "\n";
// This name is no setting at all.
string $unknown = 'my.own.setting';
echo Core\Config::set($unknown, '1') ? 'changed' : 'not allowed', "\n";
// A memory limit is a size, so this value cannot be read as one.
echo Core\Config::set('memory', 'as much as you like') ? 'changed' : 'not allowed', "\n";
Output
not allowed
not allowed
not allowed

Asking for more than the server allows

<?nvs
// Shows a job asking for more memory than it is allowed. The server may keep a
// ceiling for a limit, and a request cannot go above it.
// This prints: 8G is more than this server allows.
if (Core\Config::set('memory', '8G')) {
echo 'this job may use 8G', "\n";
} else {
echo '8G is more than this server allows', "\n";
}
// A value under the ceiling is allowed, so the job runs in smaller parts.
Core\Config::set('memory', '384M');
echo 'the job runs inside ', Core\Config::get('memory') ?? 'no limit', "\n";
Output
8G is more than this server allows
the job runs inside 384M