PHP请求生命周期 [英] PHP Request Lifecycle

查看:129
本文介绍了PHP请求生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我对PHP VM的了解还比较幼稚,最近我一直在想一些事情.特别是,对于Web应用程序,请求生命周期在PHP中看起来是什么样的.我在此处找到了一篇很好的解释,但我觉得故事的更多内容.

Okay, so I'm relatively naive in my knowledge of the PHP VM and I've been wondering about something lately. In particular, what the request lifecycle looks like in PHP for a web application. I found an article here that gives a good explanation, but I feel that there has to be more to the story.

根据本文的解释,每次向服务器发出请求时,便会解析并执行脚本!在我看来,这太疯狂了!

From what the article explains, the script is parsed and executed each time a request is made to the server! This just seems crazy to me!

我正在尝试通过编写一些利用许多PHP 5.3/5.4功能的微型框架来学习PHP.因此,我必须考虑 static 的含义以及静态类变量实际存在的时间.我希望我的应用程序可以有一个 setup 阶段,该阶段可以将其结果缓存到具有 static 属性的类中.但是,如果整个脚本都在每个请求上进行了解析和执行,那么我将无法避免如何避免为服务器上的每个请求运行应用程序初始化步骤!

I'm trying to learn PHP by writing a little micro-framework that takes advantage of many PHP 5.3/5.4 features. As such, I got to thinking about what static means and how long a static class-variable actually lives. I was hoping that my application could have a setup phase which was able to cache its results into a class with static properties. However, if the entire script is parsed and executed on each request, I fail to see how I can avoid running the application initialization steps for every request servered!

我只是真的希望我在这里错过了一些重要的事情……任何见识都倍受赞赏!

I just really hope that I am missing something important here... Any insight is greatly apreciated!

推荐答案

根据本文的解释,每次向服务器发出请求时,便会解析并执行脚本!在我看来这太疯狂了!

From what the article explains, the script is parsed and executed each time a request is made to the server! This just seems crazy to me!

不,那篇文章是准确的.有各种方法来缓存解析/编译的结果,但是脚本是每次都完整执行.在所有请求中都不会保留类或静态变量的实例.本质上,每个请求都会获得一个全新的,从未执行过的应用程序副本.

No, that article is accurate. There are various ways of caching the results of the parsing/compilation, but the script is executed in its entirety each time. No instances of classes or static variables are retained across requests. In essence, each request gets a fresh, never-before execute copy of your application.

我看不到如何避免为每个服务器请求运行应用程序初始化步骤!

I fail to see how I can avoid running the application initialization steps for every request servered!

您不能,也不应该.您需要为每个请求将应用程序初始化为空白状态.您可以将一堆数据序列化为$_SESSION,这些 会在请求中持久存在,但是您不应该这样做,直到您发现确实有必要这样做为止.

You can't, nor should you. You need to initialize your app to some blank state for each and every request. You could serialize a bunch of data into $_SESSION which is persisted across requests, but you shouldn't, until you find there is an actual need to do so.

我只是真的希望我在这里错过了一些重要的事情...

I just really hope that I am missing something important here...

您似乎什么都不担心.默认情况下,世界上每个PHP网站都以这种方式运行,并且绝大多数无需担心性能问题.

You seem to be worried over nothing. Every PHP site in the world works this way by default, and the vast, vast majority never need to worry about performance problems.

这篇关于PHP请求生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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