I wanted to set the exit code for a CLI application from a shutdown function by using a class property,
Unfortunataly (as per manual) "If you call exit() within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called."
As a result if I call exit in my shutdown function I will break other shutdown functions (like one that logs fatal errors to syslog)
However! (as per manual) "Multiple calls to register_shutdown_function() can be made, and each will be called in the same order as they were registered."
As luck would have it you are also able to register a shutdown function from within a shutdown function (at least in PHP 7.0.15 and 5.6.30)
in other words if you register a shutdown function inside a shutdown function it is appended to the shutdown function queue.
<?php
class SomeApplication{
private $exitCode = null;
public function __construct(){
register_shutdown_function(function(){
echo "some registered shutdown function";
register_shutdown_function(function(){
echo "last registered shutdown function";
exit($this->exitCode === null ? 0 : $this->exitCode);
});
});
}
}
?>