Does static variables in php persist across the requests?

后端 未结 3 1272
醉酒成梦
醉酒成梦 2020-12-29 20:19

Static variable gotcha in php

I am from Java background and have switched to php for one project recently. I have found one unexpected behaviour in php.

3条回答
  •  难免孤独
    2020-12-29 21:15

    If you begin working with complex data sets across sessions you may want to look into storing data in objects that get serialized to the database and drawn out on session restore.

    Variables in PHP aren't meant to be persistent. The flow of your application (the stack) is executed start to finish on each page run. There is nothing living in the background that continues your logic or application. The closest thing is a session but you do not want to store information like db access etc. in there.

    Your database configurations should be in some sort of config or environment file that are accessed one time to connect to the database, once a connection has been made you can simply query whenever needed and use the connection handle to identify what connection to use.

提交回复
热议问题