Home > Software design >  How it works static methods and variables
How it works static methods and variables

Time:11-02

how do static methods and static variables work on the web? Because it preserves the value inside static variables. Now I have a website and I pay, the information about the payment is in the static variable. Does this create a problem for the multi-user platform?

CodePudding user response:

In PHP, every request spawns a completely new (short-lived) environment in which your code executes. If you'd save payment information in a variable only (doesn't even matter whether it's static or not), it would cease to exist after the request completed and the user got their response. This is why it doesn't matter whether there are multiple users, since on every single request to your server, a new, fresh, state is used anyway and no information from previous (or concurrent) requests is available to you at that point.

In order to persist information, you would have to store it in some place that survives your request lifecycle, such as a database. You would store the user's details in session data (which is a place for per-user data that also persists across requests but is still short-lived, it exists to store some information across different requests for the same user while they are browsing the page) when they log in or first use the site, and knowing who they user are, you would store things like payment data in your database associated with the user's ID or name. Next time (or even years later) you would be able find the same stored data again by looking it up based on the same user ID or name.


Note: In other environments, things can behave differently. For example, if you had a node.js server, variables would persist across requests of users - also different ones - but would vanish when your server process is closed. The solution is the same though, to store things that should persist permanently in some kind of database.

CodePudding user response:

Static properties are accessed using the Scope Resolution Operator (::) and cannot be accessed through the object operator .

It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self, parent and static).

Why do you store the payment information in a static variable?

... but all content generated by the website is always for one user only. There are also as many static variables, how many users are currently on the site..

  • Related