Core\Script::finish
Core\Script::finish(): voidCore\Script::finish() ends the script at once, from any place in the code. The call does not return, and no code after it runs. The exit status is 0, because a script that finished did not fail.
Before the script ends, every finally block between the call and the top of the script runs, so files and connections are closed. No catch block catches it, not even a catch for Throwable, so error handling never mistakes it for an error. After the finally blocks, the functions added with Core\Script::onExit() run, and their report has the reason Finish.
On a server, the response that the handler already set is sent. Work added with Core\Task::runAfterResponse still runs.
The examples below show a job that stops when there is nothing to do. Then they show the finally blocks that run before the script ends. The last one answers a request at once while the site is under maintenance.
Return value
Returns void. Nothing. The call does not return, no code after it runs, and no catch block catches it. The Core\Script\ExitReport has the reason Finish, the status 0 and no error. On a server, the response that the handler set is sent, and work added with Core\Task::runAfterResponse still runs. Under nvs run, the script ends.
Examples
Stop when there is nothing to do
<?nvs// Shows how to end a script early when its work is already done.// `Core\Script::finish()` ends the script, and the exit status is 0.
array<string> $newOrders = [];
if (Core\Arr::count($newOrders) == 0) { echo "no new orders\n"; Core\Script::finish();}
// This line does not print, because the script has ended.echo "sending the invoices\n";no new ordersFinally runs and catch does not
<?nvs// Shows that `Core\Script::finish()` runs every `finally` block on its way out.// A `catch` block does not catch it, even a `catch` for `Throwable`.
try { try { echo "writing the report\n"; Core\Script::finish(); } catch (Throwable $error) { // This line does not print. Finishing is not an error. echo 'caught: ', $error->message, "\n"; } finally { // This prints "closing the report file". echo "closing the report file\n"; }} finally { // This prints too, after the inner `finally` block. echo "closing the connection\n";}
echo "this line does not print\n";writing the reportclosing the report fileclosing the connectionAnswer early in maintenance mode
<?nvs// Shows a request handler that answers at once while the site is under maintenance.// The access log is written by a function added with `Core\Script::onExit()`.
Core\Script::onExit(fn(Core\Script\ExitReport $report): void => { // After `finish()`, the reason is `Finish` and the status is 0. string $how = $report->reason() == Core\Script\ExitReport::Reason::Finish ? 'finished early' : 'ran to the end'; echo 'access log: ', $how, ', status ', $report->status(), "\n";});
class Site{ public static function handle(bool $maintenance): void { if ($maintenance) { echo "503 the site is under maintenance\n"; Core\Script::finish(); } echo "200 the page\n"; }}
Site::handle(true);echo "this line does not print\n";503 the site is under maintenanceaccess log: finished early, status 0