PHP Request Lifecycle

前端 未结 3 558
深忆病人
深忆病人 2021-01-11 23:27

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

相关标签:
3条回答
  • 2021-01-11 23:54

    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!

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

    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.

    0 讨论(0)
  • 2021-01-12 00:02

    No, you are not missing anything. If you need to keep some application state, you must do it using DB, files, Memcache etc.

    As this can sound crazy if you're not used to it, it's sometimes good for scaling and other things - you keep your state in some other services, so you can easily run few instances of PHP server.

    0 讨论(0)
  • 2021-01-12 00:12

    A static variable, like any other PHP variable only persists for the life of the script execution and as such does not 'live' anywhere. Persistence between script executions is handled via session handlers.

    0 讨论(0)
提交回复
热议问题