Imagine that the server is almost identical at the same time request.

The server, when generating the response, uses a static array of the class, for convenient access to it from different places of the code.

Is there any chance that data intended for different clients will fall into this static array?

P.S. I'm sure not, but I can't prove it.

  • No static arrays for different clients should be in principle ....... static is used for common things ..... for example, filter user data .... or convert a date from one to another, etc ... .......... for the user should be limited to an object to which there is no access for another - Alexey Shimansky
  • @ Alexey Shimansky If a new object is created, does this give a 100% guarantee that it will only be used by this user? And, for example, access to this object, for convenience, can be done through a static property (you get something like a singleton)? - RostD

1 answer 1

No, they can not.

PHP follows the Shared nothing approach, i.e. there are no shared data between requests. PHP guarantees that unless you yourself enter a common entity for queries — for example, external files, session mechanism, DBMS, and so on — parallel queries will not influence each other at all, as if they did not exist and you have only one query.

This is achieved by using different address spaces (and usually different OS threads; each process of a commonly used FPM only executes one request at a time) to process each user request. Therefore, it is necessary to launch an application for each new request from scratch, and after the request is completed, all resources are released. This is both a plus and a minus. Plus - you can not think about competitive access (until you touch external systems in relation to PHP), which greatly simplifies the code and eliminates concurrency errors. The forced release of memory after the request allows you to not worry about small memory leaks that are important for long-running services - a leak of 1kb per minute in a month is already a decent thing. This approach also allows the application to scale linearly without problems by adding new CPU cores, new processors and entirely new servers. Minus - launching an application from scratch to every request is still not free and this is a rather useless operation in its essence.