开发者

How to determine that a PHP script is in termination phase?

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?

UPDATE

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?

UPDATE2

To clarify even more, I'm still not looking for connection sta开发者_C百科te 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.


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.

<?php

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

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

$test = new Test;

#EOF; Script ends here.


You are looking for:

Connection_aborted();

http://it.php.net/manual/en/function.connection-aborted.php

or

Connection_status();

http://it.php.net/manual/en/function.connection-status.php

Addendum

There can't be any Terminated status, because if it's terminated you can't check its status lol


I have never made (practical) use of it myself yet, but you might be able to make use of:

http://www.php.net/manual/en/function.register-tick-function.php

Using this means you can write a file or update a db or something while script is running... i.e. write a record session/some id and a timestamp id to a file or something and check for time between execution perhaps, you could say if it's not been updated in X seconds it's still running.

But as stated PHP is stateless so it's not a notion that PHP will be aware of.

Failing this, you could set a DB field in some way when a script starts/just before it 'ends', but would place a lot of overhead really.


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

No, PHP is stateless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜