Skip to content

[errors]

errors.deprecated

What happens when a program uses deprecated code while it runs. Code is deprecated when it carries the attribute #[Core\Deprecated]. You set it in nvs.toml as the key deprecated in the block [errors].

There are three values:

  • ignore runs the deprecated code as usual. This is the default.
  • log runs the code and writes one warning to the log for each place a request uses it.
  • throw throws Core\DeprecatedError. Its message names the method or constant and what to use instead.

The compiler also prints a warning for each use of deprecated code it finds. This setting is about what happens when that code runs. A good use is throw in a test run, so every use of old code stops the test.

A program can change the value for its own request with Core\Config::set('errors.deprecated', 'throw'). The next request starts with the value from nvs.toml again.

The example below runs a test with throw, finds a call to an old method, and then turns the setting off for its own request.

nvs.toml sets the value that each request starts with. A request may change its own value with Core\Config::set, up to the ceiling where the key has one.

A change in the file takes effect a few seconds after you save it. The server does not restart.

The examples below run with this configuration file.

nvs.toml
# Each use of deprecated code throws `Core\DeprecatedError`.
[errors]
deprecated = "throw"

A test run that stops at old code

<?nvs
// Shows a test run that stops at each use of deprecated code.
class Mailer
{
#[Core\Deprecated(since: '3.0', replace: 'Mailer::send($to)')]
public static function mail(string $to): string
{
return Mailer::send($to);
}
public static function send(string $to): string
{
return 'sent to ' . $to;
}
}
// `nvs.toml` sets `deprecated` to `throw`, so this call throws.
try {
echo Mailer::mail('ana@example.com'), "\n";
} catch (Core\DeprecatedError $old) {
echo $old->message, "\n";
}
// This prints "sent to ana@example.com".
echo Mailer::send('ana@example.com'), "\n";
// A program may change the setting for its own request. With `ignore`, the old method runs.
Core\Config::set('errors.deprecated', 'ignore');
echo Mailer::mail('ben@example.com'), "\n";
Output
`Mailer::mail()` is deprecated since 3.0. Use `Mailer::send($to)` instead.
sent to ana@example.com
sent to ben@example.com