3

Is there a way to know the value passed to exit() from within a shutdown function?

For example:

<?php

function handleShutdown(){
    // Is there a way to know the value passed to exit() at this point?

    someCleanup();
}

register_shutdown_function('handleShutdown');

if(somethingWentWrong()){
    exit(3);
}

exit(0);

                                          

This is in the context of a complex set of legacy scripts running in CLI mode. I am looking for a solution to instrument their exit status within php that does not involve modifying all the points where the scripts invoke exit() (so, not using a global variable, define or forking the whole script).

There is this other SO question asking to know whether the exit was is "clean", but it does not address the exit code at all.

istepaniuk
  • 4,016
  • 2
  • 32
  • 60

1 Answers1

3

You can install uopz, which will give you uopz_get_exit_status().

Then use it in your handleShutdown function:

$exitCode = uopz_get_exit_status();
Joundill
  • 6,828
  • 12
  • 36
  • 50
  • 2
    The existence of `uopz_get_exit_status()` seems to prove that there is no native way. Unfortunately, I cannot easily install uopz at the moment, but this seems to be a good plan B. – istepaniuk Jan 20 '21 at 23:40