如何确定PHP脚本处于终止阶段? [英] How to determine that a PHP script is in termination phase?

查看:126
本文介绍了如何确定PHP脚本处于终止阶段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

register_shutdown_function()是否有其他选择?否则,如何处理该功能的缺点?

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

更新

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

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

要澄清更多,我仍然没有在寻找连接状态 但是,以便获得与运行状态相当的结果.它也应该在没有任何连接状态的CLI中正常工作,因为没有与执行代码有关的TCP连接-可以更好地说明我在寻找什么.

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.

推荐答案

阅读了PHP源代码的大部分内容后,我得出的结论是,即使在经验水平上也存在这样的状态,但实际上并没有以标志或变量的形式存在于解释器中.

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.

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

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.

这篇关于如何确定PHP脚本处于终止阶段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆