7

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 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.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 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. – Richard Knop Jun 03 '11 at 14:20
  • 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?"](http://stackoverflow.com/q/6227000/367456) 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. – hakre Jun 03 '11 at 14:44

4 Answers4

4

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.
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    X-Ref: [When will __destruct not be called in PHP?](http://stackoverflow.com/q/2385047/367456) – hakre Oct 16 '12 at 15:38
  • 1
    [Link to php-src](http://lxr.php.net/xref/PHP_5_4/main/main.c#1710) The shutdown procedure is pretty well documented. Starting with disabling ticks (relevant to the answer by @Brian), calling shutdown functions, and then calling object destructors. So the reliable way to detect if an object is being destroyed due to shutdown is as you have documented in your answer. – Leigh Oct 16 '12 at 16:19
  • 1
    There is a flag, but it's not accessible from userland. https://github.com/php/php-src/blob/c6982995504b6e21e8a5ade29cfb16a55196dc43/main/main.c#L1808 – kelunik Mar 14 '17 at 07:02
0

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

dynamic
  • 46,985
  • 55
  • 154
  • 231
  • No, I'm specifically not looking for the connection state related functions, but for something like run_state related function(s), variable(s) or flag(s) (if any). I thought this would have been clear from my question, will check the wording. – hakre Jun 03 '11 at 13:27
  • @hakre: There can't be any Terminated status, because if it's terminated you can't check its status lol! – dynamic Jun 03 '11 at 13:29
  • @yes123: code within destructors of object still in memory at the scripts code execution end (I named everything after that point termination phase) get executed within that termination phase. So to say, I can still execute code after the script has ended. – hakre Jun 03 '11 at 13:37
  • @hakre: you can check it with connection_status(); – dynamic Jun 03 '11 at 13:40
  • @yes123: connection_status() returns 0 in my case (using CLI that's probably why), so connection_status() provides no data I can work with at all. 0 is the normal state. – hakre Jun 03 '11 at 13:44
  • there isn't the distinction you are looking for in php – dynamic Jun 03 '11 at 13:48
  • Oh there must be, for example, Exceptions aren't thrown in that state of the program. So at least the interpreter does exactly know about the run state. Question is, how to find out about that state? – hakre Jun 03 '11 at 14:03
  • @hakre: It's probably kept internal deliberately, as an abstraction. – Lightness Races in Orbit Jun 15 '11 at 10:32
  • @Tomalak: Internal it's various flags, scanned through the PHP source. There is not much to do about, see as well my answer. – hakre Jun 15 '11 at 10:36
0

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.

Brian
  • 8,418
  • 2
  • 25
  • 32
-1

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

No, PHP is stateless.

Yeroon
  • 3,223
  • 2
  • 22
  • 29
  • 1
    According to the PHP manual, PHP is not statless. For example there are three [connection states](http://www.php.net/manual/en/features.connection-handling.php). But I'm not looking for connection states (CLI code). – hakre Jun 03 '11 at 14:07