(PECL uopz 5, PECL uopz 6, PECL uopz 7)
uopz_allow_exit — Allows control over disabled exit opcode
By default uopz disables the exit opcode, so exit() calls are practically ignored. uopz_allow_exit() allows to control this behavior.
allow
Whether to allow the execution of exit opcodes or not.
No value is returned.
Example #1 uopz_allow_exit() example
<?php
exit(1);
echo 1;
uopz_allow_exit(true);
exit(2);
echo 2;
?>
The above example will output:
1
OPcache optimizes away dead code after unconditional exit.
PHP 8.4 changed exit and die to functions, so this call has no effect. Instead you can redefine those functions to do nothing.
<?php
function allow_exit( bool $allow ) {
if ( PHP_VERSION_ID < 80400 ) {
uopz_allow_exit( $allow );
} elseif ( $allow ) {
uopz_unset_return( 'exit' );
uopz_unset_return( 'die' );
} else {
uopz_set_return('exit', static function() {});
uopz_set_return('die', static function() {});
}
}
?>
Test:
<?php
echo "Calling die() with allow_exit(false)\n";
allow_exit( false );
die();
echo "Calling die() with allow_exit(true)\n";
allow_exit( true );
die();
echo "Unreachable\n";
?>
Calling die() with allow_exit(false)
Calling die() with allow_exit(true)