如何判断PHP脚本是否处于终止阶段?

发布于 2024-11-13 08:57:11 字数 911 浏览 3 评论 0原文

PHP 中是否有任何函数/全局变量可以返回脚本的当前状态(例如运行、终止)?

或者是通过使用 设置此状态的唯一方法register_shutdown_function()

该函数对我来说看起来不灵活,因为已经注册的关闭函数可以用它覆盖。 当用户中止连接时执行关闭函数 ,这不是我明确寻找的,我不想引入太多限制。

有没有 register_shutdown_function() 的替代方案可用?或者如果没有,如何处理该功能的缺点?

更新

只是澄清一下:我不是在寻找连接状态(例如connection_aborted()),而是在寻找 PHP 脚本的运行状态(正在运行、正在终止)。用于查找有关我已经知道的连接状态的更多信息的函数,但是脚本的当前状态怎么样?脚本是否已经终止并且对象是否因此而被销毁?

UPDATE2

为了澄清更多,我仍然没有寻找连接状态 但是关于运行状态的类似内容。它也应该在 CLI 中工作,它没有任何连接状态,因为没有与执行代码相关的 TCP 连接 - 以更好地说明我正在寻找的内容。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

醉生梦死 2024-11-20 08:57:11

在阅读了大部分 PHP 源代码后,我得出的结论是,即使这种状态在经验层面上存在,它们也不会真正以标志或变量的形式存在于解释器中。

例如,有关抛出异常的代码决定各种变量是否可能。

因此,问题的答案是否定的。

到目前为止,我能找到的最好的解决方法是为此设置一个全局变量,该变量在注册的关闭函数中设置。但 PHP 的标志似乎并不真正可用。

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

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.
留蓝 2024-11-20 08:57:11

您正在寻找:

Connection_aborted();

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

Connection_status();

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

附录

不能有任何终止状态,因为如果它终止了,你就无法检查其状态哈哈

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

宫墨修音 2024-11-20 08:57:11

我自己还没有(实际)使用过它,但您也许可以使用:

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

使用这意味着您可以在同时写入文件或更新数据库或其他内容脚本正在运行...即写入记录会话/一些 id 并文件或其他内容的时间戳 ID,并检查执行之间的时间,您可以说如果它在 X 秒内没有更新,它仍然在运行。

但如前所述,PHP 是无状态的,因此 PHP 不会意识到这一概念。

如果做不到这一点,您可以在脚本启动时/在其“结束”之前以某种方式设置数据库字段,但这确实会产生大量开销。

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.

温柔女人霸气范 2024-11-20 08:57:11

是否有任何函数/全局
PHP 中的变量返回
脚本的当前状态(某些
比如运行、终止)?

不,PHP 是无状态的。

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

No, PHP is stateless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文