2

how can I exit from a php script (for example with the exit() function) but without triggering all previously registered shutdown functions (with register_shutdown_function)?

Thanks!

EDIT: alternatively, is there a way to clear from all the registered shutdown functions?

3
  • Ensure that the first registered shutdown function contains a path that calls exit() Commented Feb 19, 2013 at 11:10
  • I am working with a very complex system, and I don't actually know which is the first registered shutdown function. I just would like to exit without calling the shutdown functions, so I can debug easily. Commented Feb 19, 2013 at 11:13
  • 3
    If you want to debug easily, use a debugger (like xdebug) where you can set breakpoints and inspect values, etc Commented Feb 19, 2013 at 11:14

3 Answers 3

8

Shutdown functions will not be executed if the process is killed with a SIGTERM or SIGKILL signal.

posix_kill(posix_getpid(), SIGTERM);
Sign up to request clarification or add additional context in comments.

Comments

4

Don't use register_shutdown_function directly. Create a class which manage all shutdown functions and which has his own function and an enable property.

class Shutdown {

    private static $instance = false;
    private $functions;
    private $enabled = true;

    private function Shutdown() {
        register_shutdown_function(array($this, 'onShutdown'));
        $this->functions = array();
    }

    public static function instance() {
        if (self::$instance == false) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function onShutdown() {
        if (!$this->enabled) {
            return;
        }

        foreach ($this->functions as $fnc) {
            $fnc();
        }
    }

    public function setEnabled($value) {
        $this->enabled = (bool)$value;
    }

    public function getEnabled() {
        return $this->enabled;
    }

    public function registerFunction(callable $fnc) {
        $this->functions[] = $fnc;
    }

}

Comments

0

From https://www.php.net/manual/en/function.register-shutdown-function.php

If you call exit() within one registered shutdown function, processing will stop completely and no other registered shutdown functions will be called.

Add a new handler to the very top of the code with the code:

exit();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.