You can unregister closure-function:
declare(ticks = 1000);
$startTime = microtime(true);
$tick = true;
$closure = function () use ($startTime, &$tick) {
if (((microtime(true) - $startTime) > 5) && $tick) {
$tick = false;
throw new \Exception('Time to run code left');
}
};
try {
register_tick_function($closure);
//do your code
$result = 1;
return $result;
} catch (\Exception $e) {
throw $e;
} finally {
unregister_tick_function($closure);
}