Is there any function / global variable in PHP that returns the current state of the script (something like runnning, terminating)?

Or is the only way to set this state by making use of register_shutdown_function()?

That function looks inflexible to me as an already registered shutdown functions can be overriden with it. And the shutdown function gets executed when a user aborts the connection, which is not what I'm looking for explicitly and I don't want to introduce too many constraints.

Are there any alternatives to register_shutdown_function() available? Or if not, how to deal with the shortcomings of that function?


Just to clarify: I'm not looking for connection state (e.g. connection_aborted()) but for the run state of the PHP script (running, terminating). Functions to find out more about the connection state I already know of, but how about the current state of the script? Has the script already been terminated and are objects (going to be) destroyed because of that?


To clarify even more, I'm still not looking for connection state but for something comparable regarding the run-state. It should work in CLI as well which does not have any connection state as there is no TCP connection related to executing the code - to better illustrate what I'm looking for.


A little fff topic but you are doing something very very wrong (PHP is not like Java or C++). Why do you need to determine whether the script is in termination phase? I have never needed to do so. If you don't mind me asking.

Written by Richard Knop

I don't mind the asking at all. This question is related to "how to trigger user error with trigger_error in an object destructor while the script shuts down?" which probably gives an impression for what it can be used. I know it's somehow specifically but as the language offers some features (namely Exception throwing and - as it looks like undocumented - error triggering) based on state I need to know about that.

Written by hakre

Accepted Answer

After reading a larger part of the PHP sourcecode I came to the conclusion that even if such state(s) exist on the level of experience, they do not really exist within the interpreter in form of a flag or variable.

The code about throwing Exceptions for example decides on various variables if that is possible or not.

The answer to the question is no therefore.

The best workaround I could find so far is to have a global variable for this which is set in a registered shutdown function. But a flag from PHP seems to be not really available.


register_shutdown_function(function() {$GLOBALS['shutdown_flag']=1;});

class Test {
    public function __destruct() {
            && var_dump($GLOBALS['shutdown_flag'])

$test = new Test;

#EOF; Script ends here.
Written by hakre
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki