确认PHP静态变量不会在请求中持久存在 [英] Confirmation that PHP static variables do not persist across requests

查看:139
本文介绍了确认PHP静态变量不会在请求中持久存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找确保在PHP请求之间不存储静态变量的保证.先前的以下问题:

I am looking for assurance that static variables are NOT stored between PHP requests. The following previous questions:

跨多个.php页面的PHP静态变量

PHP中的静态变量是否在请求中持续存在? /a>

Does static variables in php persist across the requests?

跨会话的静态变量

显然不是,但是它们更多是在提供一种维持状态的方法,而不是针对预期行为的具体讨论.

clearly say that they aren't but they are more in the context of providing a way to maintain state rather than a specific discussion of what is the expected behaviour.

例如,如果我有如下的PHP代码:

As an example, if I have PHP code as follows:

function myfunc()
{
    static $a=0;
    print $a++;
}

for ($i=0;$i<10;$i++) myfunc();

然后每次运行它都会得到0123456789的输出.我对PHP的直觉/理解使我相当确定这是必须的.

then I get output of 0123456789 every time I run it. My instinct/understanding of PHP makes me fairly sure that this has to be the case.

在我自己的实验中,我将一个(preforking)apache关闭给一个孩子,以确保在两次请求之间不记得该变量.正如我所期望的,在两次请求之间它没有被记住.但这只是PHP运行的一种情况.

In my own experiments, I've shut a (preforking) apache down to one child to make sure that the variable isn't remembered between requests. It isn't remembered between requests as I'd expect. But this is only one scenario in which PHP runs.

我正在寻找的是:

指向官方文档的链接,该链接说这是预期的行为,不会改变. PHP文档的相关部分没有明确提及(除非在评论中).

A link to an official piece of documentation that says this is the expected behaviour and won't change. The relevant piece of PHP documentation doesn't mention this explicitly (except in the comments).

或者,举例说明何时在请求(例如Web服务器或性能增强的PHP框架)之间记住静态变量时,这些变量可能不会清除静态变量以提高请求之间的速度.

Or, an example of when static variables are remembered across requests such as webservers or performance enhancing PHP frameworks out there which will potentially not clear static variables to increase speed between requests.

推荐答案

PHP不会在请求之间保留应用程序状态.在PHP应用程序的生命周期中,将根据每个请求重新执行该应用程序.静态变量用于在执行离开作用域时将其值保留在局部函数作用域中.在文档中没有任何地方提到静态变量旨在保留请求中的值.

PHP does not preserve the application state between requests. During an PHP applications life cycle the application is freshly executed with each request. Static variables are meant to preserve the value of a variable in a local function scope when execution leaves the scope. Nowhere in the documentation does it mention that static variables are meant to preserve value across requests.

这篇关于确认PHP静态变量不会在请求中持久存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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